{"id":678,"date":"2012-10-28T18:07:02","date_gmt":"2012-10-28T10:07:02","guid":{"rendered":"http:\/\/blog.my0513.com\/?p=678"},"modified":"2012-10-28T18:07:02","modified_gmt":"2012-10-28T10:07:02","slug":"javascript%e6%a8%a1%e5%9d%97%e5%8c%96%e7%bc%96%e7%a8%8b%ef%bc%88%e4%b8%80%ef%bc%89%ef%bc%9a%e6%a8%a1%e5%9d%97%e7%9a%84%e5%86%99%e6%b3%95-via-%e9%98%ae%e4%b8%80%e5%b3%b0","status":"publish","type":"post","link":"https:\/\/blog.my0513.com\/?p=678","title":{"rendered":"Javascript\u6a21\u5757\u5316\u7f16\u7a0b\uff08\u4e00\uff09\uff1a\u6a21\u5757\u7684\u5199\u6cd5 (via \u962e\u4e00\u5cf0)"},"content":{"rendered":"<h1 id=\"page-title\">Javascript\u6a21\u5757\u5316\u7f16\u7a0b\uff08\u4e00\uff09\uff1a\u6a21\u5757\u7684\u5199\u6cd5<\/h1>\n<div id=\"share_button\"><\/div>\n<div>\n<p>\u4f5c\u8005\uff1a\u00a0<a href=\"http:\/\/www.ruanyifeng.com\/\">\u962e\u4e00\u5cf0<\/a><\/p>\n<p>\u65e5\u671f\uff1a\u00a0<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2012\/10\/\">2012\u5e7410\u670826\u65e5<\/a><\/p>\n<\/div>\n<div id=\"main-content\">\n<p>\u968f\u7740\u7f51\u7ad9\u9010\u6e10\u53d8\u6210&#8221;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Web_application\" target=\"_blank\">\u4e92\u8054\u7f51\u5e94\u7528\u7a0b\u5e8f<\/a>&#8220;\uff0c\u5d4c\u5165\u7f51\u9875\u7684Javascript\u4ee3\u7801\u8d8a\u6765\u8d8a\u5e9e\u5927\uff0c\u8d8a\u6765\u8d8a\u590d\u6742\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.beekka.com\/blog\/201210\/bg2012102601.jpg\" alt=\"\" \/><\/p>\n<p>\u7f51\u9875\u8d8a\u6765\u8d8a\u50cf\u684c\u9762\u7a0b\u5e8f\uff0c\u9700\u8981\u4e00\u4e2a\u56e2\u961f\u5206\u5de5\u534f\u4f5c\u3001\u8fdb\u5ea6\u7ba1\u7406\u3001\u5355\u5143\u6d4b\u8bd5\u7b49\u7b49&#8230;&#8230;\u5f00\u53d1\u8005\u4e0d\u5f97\u4e0d\u4f7f\u7528\u8f6f\u4ef6\u5de5\u7a0b\u7684\u65b9\u6cd5\uff0c\u7ba1\u7406\u7f51\u9875\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<p>Javascript\u6a21\u5757\u5316\u7f16\u7a0b\uff0c\u5df2\u7ecf\u6210\u4e3a\u4e00\u4e2a\u8feb\u5207\u7684\u9700\u6c42\u3002\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u5f00\u53d1\u8005\u53ea\u9700\u8981\u5b9e\u73b0\u6838\u5fc3\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u5176\u4ed6\u90fd\u53ef\u4ee5\u52a0\u8f7d\u522b\u4eba\u5df2\u7ecf\u5199\u597d\u7684\u6a21\u5757\u3002<\/p>\n<p>\u4f46\u662f\uff0cJavascript\u4e0d\u662f\u4e00\u79cd\u6a21\u5757\u5316\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b83\u4e0d\u652f\u6301&#8221;<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2012\/07\/three_ways_to_define_a_javascript_class.html\" target=\"_blank\">\u7c7b<\/a>&#8220;\uff08class\uff09\uff0c\u66f4\u9051\u8bba&#8221;\u6a21\u5757&#8221;\uff08module\uff09\u4e86\u3002\uff08\u6b63\u5728\u5236\u5b9a\u4e2d\u7684<a href=\"http:\/\/en.wikipedia.org\/wiki\/ECMAScript\" target=\"_blank\">ECMAScript\u6807\u51c6<\/a>\u7b2c\u516d\u7248\uff0c\u5c06\u6b63\u5f0f\u652f\u6301&#8221;\u7c7b&#8221;\u548c&#8221;\u6a21\u5757&#8221;\uff0c\u4f46\u8fd8\u9700\u8981\u5f88\u957f\u65f6\u95f4\u624d\u80fd\u6295\u5165\u5b9e\u7528\u3002\uff09<\/p>\n<p>Javascript\u793e\u533a\u505a\u4e86\u5f88\u591a\u52aa\u529b\uff0c\u5728\u73b0\u6709\u7684\u8fd0\u884c\u73af\u5883\u4e2d\uff0c\u5b9e\u73b0&#8221;\u6a21\u5757&#8221;\u7684\u6548\u679c\u3002\u672c\u6587\u603b\u7ed3\u4e86\u5f53\u524d\uff02Javascript\u6a21\u5757\u5316\u7f16\u7a0b\uff02\u7684\u6700\u4f73\u5b9e\u8df5\uff0c\u8bf4\u660e\u5982\u4f55\u6295\u5165\u5b9e\u7528\u3002\u867d\u7136\u8fd9\u4e0d\u662f\u521d\u7ea7\u6559\u7a0b\uff0c\u4f46\u662f\u53ea\u8981\u7a0d\u7a0d\u4e86\u89e3Javascript\u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u5c31\u80fd\u770b\u61c2\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.beekka.com\/blog\/201210\/bg2012102602.jpg\" alt=\"\" \/><\/p>\n<p><strong>\u4e00\u3001\u539f\u59cb\u5199\u6cd5<\/strong><\/p>\n<p>\u6a21\u5757\u5c31\u662f\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684\u4e00\u7ec4\u65b9\u6cd5\u3002<\/p>\n<p>\u53ea\u8981\u628a\u4e0d\u540c\u7684\u51fd\u6570\uff08\u4ee5\u53ca\u8bb0\u5f55\u72b6\u6001\u7684\u53d8\u91cf\uff09\u7b80\u5355\u5730\u653e\u5728\u4e00\u8d77\uff0c\u5c31\u7b97\u662f\u4e00\u4e2a\u6a21\u5757\u3002<\/p>\n<blockquote><p>\u3000\u3000function m1(){<br \/>\n\/\/&#8230;<br \/>\n}<\/p>\n<p>function m2(){<br \/>\n\/\/&#8230;<br \/>\n}<\/p><\/blockquote>\n<p>\u4e0a\u9762\u7684\u51fd\u6570m1()\u548cm2()\uff0c\u7ec4\u6210\u4e00\u4e2a\u6a21\u5757\u3002\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u76f4\u63a5\u8c03\u7528\u5c31\u884c\u4e86\u3002<\/p>\n<p>\u8fd9\u79cd\u505a\u6cd5\u7684\u7f3a\u70b9\u5f88\u660e\u663e\uff1a&#8221;\u6c61\u67d3&#8221;\u4e86\u5168\u5c40\u53d8\u91cf\uff0c\u65e0\u6cd5\u4fdd\u8bc1\u4e0d\u4e0e\u5176\u4ed6\u6a21\u5757\u53d1\u751f\u53d8\u91cf\u540d\u51b2\u7a81\uff0c\u800c\u4e14\u6a21\u5757\u6210\u5458\u4e4b\u95f4\u770b\u4e0d\u51fa\u76f4\u63a5\u5173\u7cfb\u3002<\/p>\n<p><strong>\u4e8c\u3001\u5bf9\u8c61\u5199\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u4e0a\u9762\u7684\u7f3a\u70b9\uff0c\u53ef\u4ee5\u628a\u6a21\u5757\u5199\u6210\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6240\u6709\u7684\u6a21\u5757\u6210\u5458\u90fd\u653e\u5230\u8fd9\u4e2a\u5bf9\u8c61\u91cc\u9762\u3002<\/p>\n<blockquote><p>\u3000\u3000var module1 = new Object({<\/p>\n<p>_count : 0,<\/p>\n<p>m1 : function (){<br \/>\n\/\/&#8230;<br \/>\n},<\/p>\n<p>m2 : function (){<br \/>\n\/\/&#8230;<br \/>\n}<\/p>\n<p>});<\/p><\/blockquote>\n<p>\u4e0a\u9762\u7684\u51fd\u6570m1()\u548cm2(\uff09\uff0c\u90fd\u5c01\u88c5\u5728module1\u5bf9\u8c61\u91cc\u3002\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u5c31\u662f\u8c03\u7528\u8fd9\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u3002<\/p>\n<blockquote><p>\u3000\u3000module1.m1();<\/p><\/blockquote>\n<p>\u4f46\u662f\uff0c\u8fd9\u6837\u7684\u5199\u6cd5\u4f1a\u66b4\u9732\u6240\u6709\u6a21\u5757\u6210\u5458\uff0c\u5185\u90e8\u72b6\u6001\u53ef\u4ee5\u88ab\u5916\u90e8\u6539\u5199\u3002\u6bd4\u5982\uff0c\u5916\u90e8\u4ee3\u7801\u53ef\u4ee5\u76f4\u63a5\u6539\u53d8\u5185\u90e8\u8ba1\u6570\u5668\u7684\u503c\u3002<\/p>\n<blockquote><p>\u3000\u3000module1._count = 5;<\/p><\/blockquote>\n<p><strong>\u4e09\u3001\u7acb\u5373\u6267\u884c\u51fd\u6570\u5199\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528&#8221;<a href=\"http:\/\/benalman.com\/news\/2010\/11\/immediately-invoked-function-expression\/\" target=\"_blank\">\u7acb\u5373\u6267\u884c\u51fd\u6570<\/a>&#8220;\uff08Immediately-Invoked Function Expression\uff0cIIFE\uff09\uff0c\u53ef\u4ee5\u8fbe\u5230\u4e0d\u66b4\u9732\u79c1\u6709\u6210\u5458\u7684\u76ee\u7684\u3002<\/p>\n<blockquote><p>\u3000\u3000var module1 = (function(){<\/p>\n<p>var _count = 0;<\/p>\n<p>var m1 = function(){<br \/>\n\/\/&#8230;<br \/>\n};<\/p>\n<p>var m2 = function(){<br \/>\n\/\/&#8230;<br \/>\n};<\/p>\n<p>return {<br \/>\nm1 : m1,<br \/>\nm2 : m2<br \/>\n};<\/p>\n<p>})();<\/p><\/blockquote>\n<p>\u4f7f\u7528\u4e0a\u9762\u7684\u5199\u6cd5\uff0c\u5916\u90e8\u4ee3\u7801\u65e0\u6cd5\u8bfb\u53d6\u5185\u90e8\u7684_count\u53d8\u91cf\u3002<\/p>\n<blockquote><p>\u3000\u3000console.info(module1._count); \/\/undefined<\/p><\/blockquote>\n<p>module1\u5c31\u662fJavascript\u6a21\u5757\u7684\u57fa\u672c\u5199\u6cd5\u3002\u4e0b\u9762\uff0c\u518d\u5bf9\u8fd9\u79cd\u5199\u6cd5\u8fdb\u884c\u52a0\u5de5\u3002<\/p>\n<p><strong>\u56db\u3001\u653e\u5927\u6a21\u5f0f<\/strong><\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u6a21\u5757\u5f88\u5927\uff0c\u5fc5\u987b\u5206\u6210\u51e0\u4e2a\u90e8\u5206\uff0c\u6216\u8005\u4e00\u4e2a\u6a21\u5757\u9700\u8981\u7ee7\u627f\u53e6\u4e00\u4e2a\u6a21\u5757\uff0c\u8fd9\u65f6\u5c31\u6709\u5fc5\u8981\u91c7\u7528&#8221;\u653e\u5927\u6a21\u5f0f&#8221;\uff08augmentation\uff09\u3002<\/p>\n<blockquote><p>\u3000\u3000var module1 = (function (mod){<\/p>\n<p>mod.m3 = function () {<br \/>\n\/\/&#8230;<br \/>\n};<\/p>\n<p>return mod;<\/p>\n<p>})(module1);<\/p><\/blockquote>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e3amodule1\u6a21\u5757\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b0\u65b9\u6cd5m3()\uff0c\u7136\u540e\u8fd4\u56de\u65b0\u7684module1\u6a21\u5757\u3002<\/p>\n<p><strong>\u4e94\u3001\u5bbd\u653e\u5927\u6a21\u5f0f\uff08Loose augmentation\uff09<\/strong><\/p>\n<p>\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\uff0c\u6a21\u5757\u7684\u5404\u4e2a\u90e8\u5206\u901a\u5e38\u90fd\u662f\u4ece\u7f51\u4e0a\u83b7\u53d6\u7684\uff0c\u6709\u65f6\u65e0\u6cd5\u77e5\u9053\u54ea\u4e2a\u90e8\u5206\u4f1a\u5148\u52a0\u8f7d\u3002\u5982\u679c\u91c7\u7528\u4e0a\u4e00\u8282\u7684\u5199\u6cd5\uff0c\u7b2c\u4e00\u4e2a\u6267\u884c\u7684\u90e8\u5206\u6709\u53ef\u80fd\u52a0\u8f7d\u4e00\u4e2a\u4e0d\u5b58\u5728\u7a7a\u5bf9\u8c61\uff0c\u8fd9\u65f6\u5c31\u8981\u91c7\u7528&#8221;\u5bbd\u653e\u5927\u6a21\u5f0f&#8221;\u3002<\/p>\n<blockquote><p>\u3000\u3000var module1 = ( function (mod){<\/p>\n<p>\/\/&#8230;<\/p>\n<p>return mod;<\/p>\n<p>})(window.module1 || {});<\/p><\/blockquote>\n<p>\u4e0e&#8221;\u653e\u5927\u6a21\u5f0f&#8221;\u76f8\u6bd4\uff0c\uff02\u5bbd\u653e\u5927\u6a21\u5f0f\uff02\u5c31\u662f&#8221;\u7acb\u5373\u6267\u884c\u51fd\u6570&#8221;\u7684\u53c2\u6570\u53ef\u4ee5\u662f\u7a7a\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u516d\u3001\u8f93\u5165\u5168\u5c40\u53d8\u91cf<\/strong><\/p>\n<p>\u72ec\u7acb\u6027\u662f\u6a21\u5757\u7684\u91cd\u8981\u7279\u70b9\uff0c\u6a21\u5757\u5185\u90e8\u6700\u597d\u4e0d\u4e0e\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u76f4\u63a5\u4ea4\u4e92\u3002<\/p>\n<p>\u4e3a\u4e86\u5728\u6a21\u5757\u5185\u90e8\u8c03\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u5fc5\u987b\u663e\u5f0f\u5730\u5c06\u5176\u4ed6\u53d8\u91cf\u8f93\u5165\u6a21\u5757\u3002<\/p>\n<blockquote><p>\u3000\u3000var module1 = (function ($, YAHOO) {<\/p>\n<p>\/\/&#8230;<\/p>\n<p>})(jQuery, YAHOO);<\/p><\/blockquote>\n<p>\u4e0a\u9762\u7684module1\u6a21\u5757\u9700\u8981\u4f7f\u7528jQuery\u5e93\u548cYUI\u5e93\uff0c\u5c31\u628a\u8fd9\u4e24\u4e2a\u5e93\uff08\u5176\u5b9e\u662f\u4e24\u4e2a\u6a21\u5757\uff09\u5f53\u4f5c\u53c2\u6570\u8f93\u5165module1\u3002\u8fd9\u6837\u505a\u9664\u4e86\u4fdd\u8bc1\u6a21\u5757\u7684\u72ec\u7acb\u6027\uff0c\u8fd8\u4f7f\u5f97\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\u53d8\u5f97\u660e\u663e\u3002\u8fd9\u65b9\u9762\u66f4\u591a\u7684\u8ba8\u8bba\uff0c\u53c2\u89c1Ben Cherry\u7684\u8457\u540d\u6587\u7ae0<a href=\"http:\/\/www.adequatelygood.com\/2010\/3\/JavaScript-Module-Pattern-In-Depth\" target=\"_blank\">\u300aJavaScript Module Pattern: In-Depth\u300b<\/a>\u3002<\/p>\n<p>\u8fd9\u4e2a\u7cfb\u5217\u7684\u7b2c\u4e8c\u90e8\u5206\uff0c\u5c06\u8ba8\u8bba\u5982\u4f55\u5728\u6d4f\u89c8\u5668\u73af\u5883\u7ec4\u7ec7\u4e0d\u540c\u7684\u6a21\u5757\u3001\u7ba1\u7406\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u6027\u3002<\/p>\n<p>\uff08\u5b8c\uff09<\/p>\n<\/div>\n<div>\n<h3>\u6587\u6863\u4fe1\u606f<\/h3>\n<ul>\n<li>\u7248\u6743\u58f0\u660e\uff1a\u81ea\u7531\u8f6c\u8f7d-\u975e\u5546\u7528-\u975e\u884d\u751f-\u4fdd\u6301\u7f72\u540d |\u00a0<a href=\"http:\/\/creativecommons.org\/licenses\/by-nc-nd\/3.0\/deed.zh\">Creative Commons BY-NC-ND 3.0<\/a><\/li>\n<li>\u539f\u6587\u7f51\u5740\uff1a<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2012\/10\/javascript_module.html\">http:\/\/www.ruanyifeng.com\/blog\/2012\/10\/javascript_module.html<\/a><\/li>\n<li>\u6700\u540e\u4fee\u6539\u65f6\u95f4\uff1a2012\u5e7410\u670828\u65e5 11:24<\/li>\n<li>\u4ed8\u8d39\u652f\u6301\uff1a<a href=\"https:\/\/me.alipay.com\/ruanyf\" target=\"_blank\"><img decoding=\"async\" title=\"\u4eba\u6c11\u5e01\" src=\"http:\/\/www.ruanyifeng.com\/blog\/images\/rmb_32.png\" alt=\"\u4eba\u6c11\u5e01 - \u652f\u4ed8\u5b9d\" \/><\/a>\u00a0|\u00a0<a href=\"https:\/\/www.paypal.com\/cgi-bin\/webscr?cmd=_xclick&amp;business=yifeng.ruan@gmail.com&amp;currency_code=USD&amp;amount=0.99&amp;return=http:\/\/www.ruanyifeng.com\/thank.html&amp;item_name=Ruan%20YiFeng's%20Blog&amp;undefined_quantity=1&amp;no_note=0\" target=\"_blank\"><img decoding=\"async\" title=\"\u7f8e\u5143\" src=\"http:\/\/www.ruanyifeng.com\/blog\/images\/dollar_32.png\" alt=\"\u7f8e\u5143 - paypal\" \/><\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Javascript\u6a21\u5757\u5316\u7f16\u7a0b\uff08\u4e00\uff09\uff1a\u6a21\u5757\u7684\u5199\u6cd5 \u4f5c\u8005\uff1a\u00a0\u962e\u4e00\u5cf0 \u65e5\u671f\uff1a\u00a02012\u5e7410\u670826\u65e5 \u968f\u7740\u7f51\u7ad9\u9010\u6e10 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83,1,32],"tags":[129],"class_list":["post-678","post","type-post","status-publish","format-standard","hentry","category-javascript","category-1","category-32","tag-javascript"],"_links":{"self":[{"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/posts\/678","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=678"}],"version-history":[{"count":2,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/posts\/678\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=\/wp\/v2\/posts\/678\/revisions\/680"}],"wp:attachment":[{"href":"https:\/\/blog.my0513.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.my0513.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}