{"id":1601,"date":"2013-11-22T22:40:12","date_gmt":"2013-11-22T14:40:12","guid":{"rendered":"http:\/\/me.doucl.com\/?p=1601"},"modified":"2013-11-22T22:40:12","modified_gmt":"2013-11-22T14:40:12","slug":"self-executing-anonymous-functions","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/front-end\/self-executing-anonymous-functions.html","title":{"rendered":"JavaScript\u81ea\u6267\u884c\u533f\u540d\u51fd\u6570\u7ed3\u5408\u95ed\u5305\u7279\u6027\u8fd0\u7528"},"content":{"rendered":"

\u7ecf\u5e38\u4f7f\u7528JavaScript\u7684\u540c\u5b66\u5e94\u8be5\u6ce8\u610f\u8fc7\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u8fd9\u6837\u7ed3\u6784\u7684\u811a\u672c\u8bed\u8a00<\/p>\n

(function(){
\n… …
\n})();<\/p><\/blockquote>\n

\u8457\u540d\u7684\u7b2c\u4e09\u65b9\u7c7b\u5e93jQuery\u5c31\u662f\u7528\u5230\u8fd9\u6837\u7684\u8bed\u6cd5\uff0c\u8fd9\u4e2a\u5c31\u53eb\u505a\u201c\u81ea\u6267\u884c\u533f\u540d\u51fd\u6570\u201d<\/p>\n

\u7b2c\u4e00\u6b21\u770b\u5230\u8fd9\u6837\u7684\u8bed\u6cd5\u7684\u65f6\u5019\u5176\u5b9e\u611f\u89c9\u633a\u5947\u602a\u7684\uff0c\u9664\u4e86\u62ec\u53f7\u91cc\u9762\u7684\u533f\u540d\u51fd\u6570\u5916\u201c()();\u201c\uff0c\u4e24\u4e2a\u62ec\u53f7\u4e14\u4ee5\u5206\u53f7\u7ed3\u5c3e\uff0c\u7740\u5b9e\u6709\u70b9\u96be\u4ee5\u7406\u89e3\u3002\u5176\u5b9e\u4ed4\u7ec6\u5206\u6790\u4e00\u4e0b\u8fd8\u662f\u53ef\u4ee5\u8bb2\u7684\u901a\u7684\u3002<\/p>\n

\u9996\u5148\uff0c\u5c0f\u62ec\u53f7\u5728\u8bed\u53e5\u4e2d\u8d77\u5230\u201d\u8868\u8fbe\u5f0f\u7ec4\u5408\u5206\u5757\u201c\u4f5c\u7528\uff0c\u5e76\u4e14\u6bcf\u4e2a\u5206\u7ec4\u90fd\u4f1a\u6709\u8fd4\u56de\u503c\uff0c\u5728\u63a7\u5236\u53f0\u4e0b\u8fd0\u884c\u6267\u884c\u4e0b\u9762\u7684\u8bed\u53e5\u8bd5\u8bd5<\/p>\n

alert(typeof function(){}); \u00a0 \/\/ “function”<\/p><\/blockquote>\n

\"\"<\/a><\/p>\n

\u53ef\u89c1\u81ea\u6267\u884c\u533f\u540d\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u62ec\u53f7\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u201cFunction\u5bf9\u8c61\u201d\uff0c\u5b9e\u9645\u4e0a\u5c0f\u62ec\u53f7\u52a0\u4e0a\u533f\u540d\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u7684\u5f15\u7528\uff0c\u540e\u9762\u518d\u52a0\u4e00\u4e2a\u62ec\u53f7\u5c31\u8ddf\u666e\u901a\u51fd\u6570\u7684\u8c03\u7528\u4e00\u6837\uff0c\u7531\u6b64\u4fbf\u53ef\u4ee5\u89e3\u91ca\u62ec\u53f7\u91cc\u9762\u7684\u533f\u540d\u51fd\u6570\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u8fbe\u5230\u81ea\u6267\u884c\u7684\u6548\u679c\u4e86<\/p>\n

\u7ed3\u5408\u95ed\u5305\u7279\u6027\uff0c\u5bf9\u5916\u5b9e\u73b0\u63a5\u53e3\u5c01\u88c5<\/strong><\/p>\n

\u901a\u5e38\u5728\u590d\u6742\u9879\u76ee\u4e2d\u4f1a\u5f15\u7528\u5230\u591a\u4e2aJavaScript\u811a\u672c\uff0c\u7279\u522b\u662f\u5728\u534f\u540c\u5de5\u4f5c\u7684\u65f6\u5019\uff0c\u6216\u8005\u9879\u76ee\u4f5c\u4e3a\u7b2c\u4e09\u65b9\u5e93\u5bf9\u5916\u53d1\u5e03\u7684\u65f6\u5019\uff0c\u5982\u679c\u4e0d\u505a\u7279\u6b8a\u5904\u7406\uff0c\u5f88\u5bb9\u6613\u51fa\u73b0\u53d8\u91cf\u3001\u51fd\u6570\u547d\u540d\u51b2\u7a81\u3002\u81ea\u6267\u884c\u533f\u540d\u51fd\u6570\u7ed3\u5408\u95ed\u5305\u7279\u6027\u53ef\u4ee5\u6709\u6548\u5b9e\u73b0\u6709\u9009\u62e9\u6027\u5730\u5bf9\u5916\u66b4\u9732\u63a5\u53e3<\/p>\n

\n
(function(){\r\n  var foo = 'Hello';\r\n  var bar = 'World!'\r\n\r\n  function baz(){\r\n      return foo + ' ' + bar;\r\n  }\r\n  function foo(){\r\n      return foo + ' ' + bar;\r\n  }\r\n\r\n  window.baz = baz; \/\/Assign 'baz' to the global variable 'baz'...\r\n})();\r\n\r\nconsole.log(baz()); \/\/...and now this works.\r\nconsole.log(foo()); \/\/... do not works.<\/pre>\n<\/blockquote>\n

\u4e0a\u9762\u7684\u4ee3\u7801\u8fd0\u884c\u7ed3\u679c\uff0cfoo()\u4e4b\u6240\u4ee5\u4e0d\u6267\u884c\u662f\u56e0\u4e3afoo()\u7684\u4f5c\u7528\u57df\u9650\u5236\uff0c\u800cbaz()\u56e0\u4e3a\u8d4b\u503c\u4f5c\u4e3awindow\u7684\u5c5e\u6027\u800c\u66b4\u9732\u5728\u533f\u540d\u51fd\u6570\u4e4b\u5916\uff0c\u8fd9\u6837\u5373\u53ef\u4ee5\u4fdd\u6301foo()\u51fd\u6570\u7684\u79c1\u6709\u7279\u6027\uff0c\u53c8\u53ef\u4ee5\u5bf9\u5916\u63d0\u4f9b\u63a5\u53e3.<\/p>\n

 <\/p>\n

 <\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

\u7ecf\u5e38\u4f7f\u7528JavaScript\u7684\u540c\u5b66\u5e94\u8be5\u6ce8\u610f\u8fc7\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u8fd9\u6837\u7ed3\u6784\u7684\u811a\u672c\u8bed\u8a00 (function(){ &#823 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[112,113],"_links":{"self":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1601"}],"collection":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/comments?post=1601"}],"version-history":[{"count":1,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1601\/revisions"}],"predecessor-version":[{"id":1602,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1601\/revisions\/1602"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1601"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}