{"id":60,"date":"2015-11-19T14:57:25","date_gmt":"2015-11-19T06:57:25","guid":{"rendered":"http:\/\/douhan.li\/?p=60"},"modified":"2015-11-19T14:57:25","modified_gmt":"2015-11-19T06:57:25","slug":"javascript%e5%8f%98%e9%87%8f%e4%bd%9c%e7%94%a8%e5%9f%9f","status":"publish","type":"post","link":"https:\/\/douhan.li\/?p=60","title":{"rendered":"Javascript\u53d8\u91cf\u4f5c\u7528\u57df"},"content":{"rendered":"<p>\u4f17\u6240\u5468\u77e5,\u5728ES6\u4e4b\u524dJS\u4e2d\u7684\u53d8\u91cf\u4f5c\u7528\u57df\u662f\u51fd\u6570\u7ea7\u7684,\u4e0d\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df,\u800c\u8fd9\u53e5\u8bdd\u4e5f\u4ec5\u9650\u4e8e\u5bf9Javascript\u8bed\u8a00\u672c\u8eab\u800c\u8a00\u7684,\u806a\u660e\u7684\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u901a\u8fc7\u95ed\u5305\u53ef\u4ee5\u521b\u5efa\u51fa\u4f2a\u5757\u7ea7\u4f5c\u7528\u57df\u7528\u6765\u6a21\u62df\u8fd9\u79cd\u5176\u4ed6\u8bed\u8a00\u4e00\u822c\u90fd\u6709\u7684\u5757\u7ea7\u4f5c\u7528\u57df.\u5c31\u50cf\u8fd9\u6837:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction foo(){\r\n for(var a=0;a&lt;10;a++){}\r\n console.log(a);\/\/10\r\n for(a=0;a&lt;10;a++){}\r\n}\r\nfoo();\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction foo(){\r\n (function(){\r\n  for(var a=0;a&lt;10;a++){}\r\n })();\r\n console.log(a);\/\/undefined\r\n}\r\nfoo();\r\n<\/pre>\n<p>\u7136\u800ces2015\u7ed9\u4f60\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u65b9\u6848:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction foo(){\r\n &quot;use strict&quot;;\r\n for(let a=0;a&lt;10;a++){}\r\n console.log(a); \/\/undefined\r\n}\r\nfoo();\r\n<\/pre>\n<p>\u4f46\u5199\u672c\u6587\u7684\u4e3b\u8981\u76ee\u7684\u4e0e\u4e0a\u9762\u7684\u5173\u7cfb\u4e0d\u662f\u592a\u5927,\u672c\u6587\u4e3b\u8981\u60f3\u63a2\u7a76\u51fd\u6570\u7ea7\u7684\u4f5c\u7528\u57df\u95ee\u9898<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction foo(){\r\n var a=0;\r\n function b(){\r\n  console.log('b');\r\n }\r\n function bar(){\r\n  console.log(a);\/\/a\r\n  b();\/\/b\r\n }\r\n bar();\r\n}\r\nfoo();\r\n<\/pre>\n<p>\u6267\u884c\u8fd9\u4e00\u6bb5\u7a0b\u5e8f\u4e4b\u540e\u53ef\u4ee5\u53d1\u73b0\u51fd\u6570\u91cc\u7684\u6240\u6709\u53d8\u91cf\u548c\u51fd\u6570\u662f\u53ef\u4ee5\u88ab\u8be5\u51fd\u6570\u4ee5\u53ca\u8be5\u51fd\u6570\u5185\u90e8\u7684\u5b50\u51fd\u6570\u6240\u8bbf\u95ee\u7684,\u7a0d\u5fae\u6539\u4e00\u4e0b\u4ee3\u7801<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction foo(){\r\n var a=0;\r\n function b(){\r\n  console.log('b');\r\n }\r\n function bar(){\r\n  console.log(a);\/\/a\r\n  b();\/\/b\r\n }\r\n bar.call(d);\r\n}\r\nfunction d(){\r\n}\r\nfoo();\r\n<\/pre>\n<p>\u53d1\u73b0\u8f93\u51fa\u7684\u7ed3\u679c\u662f\u76f8\u540c\u7684,\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u662f\u4e3a\u4e86\u8bc1\u660e\u89e3\u91ca\u5668\u5728\u67e5\u627e\u53d8\u91cf\u7684\u65f6\u5019\u4e0d\u662f\u901a\u8fc7this\u5b9a\u4f4d\u67e5\u627e\u7684,\u56e0\u4e3a\u5728\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2dthis\u4e3awindow(strict mode \u4e3aundefined),\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\u4e3ad\u51fd\u6570,\u8fd9\u4e2a\u73b0\u8c61\u7684\u5177\u4f53\u539f\u56e0\u53ef\u4ee5\u901a\u8fc7\u89c2\u5bdf\u5b9e\u9645\u7684\u4f5c\u7528\u57df\u94fe\u53bb\u89e3\u91ca.<br \/>\n\u8fd9\u6bb5\u7a0b\u5e8f\u5728\u8fd0\u884c\u5230bar()\u65f6\u4f1a\u5c06foo()\u6307\u9488\u538b\u6808,\u5f00\u59cb\u6267\u884cbar,\u4f46\u89e3\u91cabar\u53d1\u73b0\u6709\u5728foo\u4e2d\u7684\u540c\u540d\u53d8\u91cfc,\u8fd9\u6837\u5c31\u4f1a\u5c06bar\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u5f62\u6210\u4e00\u4e2a\u95ed\u5305,\u5728\u4f5c\u7528\u57df\u4e2d\u540c\u65f6\u4f1a\u51fa\u73b0\u81ea\u5df1\u7684c\u548c\u95ed\u5305\u4e2d\u7684c.<br \/>\n\u5f88\u591a\u4eba\u5305\u62ec\u6211\u5728\u8fd9\u4e2a\u5730\u65b9\u5c31\u4f1a\u6709\u4e00\u4e2a\u7591\u95ee:\u95ed\u5305\u5305\u7684\u662f\u5f15\u7528\u53d8\u91cf\u8fd8\u662f\u6574\u4e2a\u51fd\u6570?<br \/>\n\u4e0d\u7ba1\u95ed\u5305\u5305\u4e86\u5f15\u7528\u53d8\u91cf\u8fd8\u662f\u6574\u4e2a\u51fd\u6570,\u5bf9\u4e0e\u5f00\u53d1\u8005\u800c\u8a00\u5e76\u6ca1\u6709\u4ec0\u4e48\u4e24\u6837,\u56e0\u4e3a\u4f60\u5c31\u7b97\u53ea\u5269\u4e0b\u5f15\u7528\u90e8\u5206\u5269\u4f59\u90fd\u88ab\u56de\u6536\u4e86\u6211\u4e5f\u4e0d\u4f1a\u53d7\u4ec0\u4e48\u5f71\u54cd,\u800c\u8fd9\u4e2a\u95ee\u9898\u7684\u5f71\u54cd\u8303\u56f4\u53ea\u662fGC\u662f\u4e0d\u662f\u4f1a\u90e8\u5206\u56de\u6536,\u6211\u60f3\u5927\u90e8\u5206\u5f00\u53d1\u8005\u5e76\u4e0d\u4f1a\u53bb\u5173\u5fc3\u8fd9\u4e2a\u95ee\u9898.\u7136\u800c\u8fd9\u4ef6\u4e8b\u8bf7\u5176\u5b9e\u662f\u975e\u5e38\u4e25\u91cd\u7684,\u56e0\u4e3a\u73b0\u6709\u7684GC\u673a\u5236\u662f\u53ea\u8981\u5f15\u7528\u5b58\u5728,\u6574\u4e2a\u51fd\u6570\u5c31\u4e0d\u80fd\u88abGC\u56de\u6536,\u6240\u4ee5\u5b9e\u9645\u60c5\u51b5\u4e0b\u95ed\u5305\u662f\u5305\u7684\u6574\u4e2a\u51fd\u6570,\u4f46\u4f60\u5e76\u4e0d\u80fd\u8bbf\u95ee\u5230\u4e0d\u5b58\u5728\u5f15\u7528\u7684\u6210\u5458,\u8fd9\u4e5f\u975e\u5e38\u7b26\u5408\u5e38\u7406.<br \/>\n\u901a\u8fc7\u4ee5\u4e0a\u7684\u5206\u6790,\u6211\u4eec\u5c31\u53ef\u4ee5\u89e3\u91ca\u4e0b\u9762\u8fd9\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u4f8b\u5b50\u7684\u4f5c\u7528\u57df\u8303\u56f4\u4e86<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction Foo(){\r\n var a=1;\r\n this.a=2;\r\n function geta(){\r\n  &quot;use strict&quot;;\r\n  console.log(a); \/\/1\r\n  console.log(this.a); \/\/Cannot read property 'a' of undefined\r\n }\r\n this.geta=function(){\r\n  console.log(a);\/\/1\r\n  console.log(this.a);\/\/2\r\n }\r\n geta();\r\n}\r\nfoo=new Foo();\r\nfoo.geta();\r\n<\/pre>\n<p>\u603b\u4f53\u6765\u8bf4,\u53ef\u4ee5\u7b80\u5355\u6982\u62ec\u6210\u4e00\u53e5\u8bdd:this\u662fthis,\u4f5c\u7528\u57df\u662f\u4f5c\u7528\u57df.\u4e0d\u80fd\u5c06this\u548c\u4f5c\u7528\u57df\u6df7\u6dc6,\u8fd9\u6837\u4ee5\u6765\u54ea\u4e9b\u53ef\u4ee5\u8bbf\u95ee\u5230,\u54ea\u4e9b\u4e0d\u80fd\u88ab\u8bbf\u95ee,\u5f88\u5bb9\u6613\u5c31\u80fd\u533a\u5206\u51fa\u6765,\u4e5f\u80fd\u5f88\u5feb\u7406\u89e3\u4e3a\u5565\u597d\u591a\u5176\u4ed6\u4eba\u5199\u7684\u7a0b\u5e8f\u91cc\u9762\u4f1a\u51fa\u73b0apply\u6216call,\u8fd9\u80af\u5b9a\u662f\u56e0\u4e3a\u91cc\u9762\u7528\u5230\u4e86this,\u800c\u548c\u4f5c\u7528\u57df\u5c31\u626f\u4e0d\u4e0a\u4efb\u4f55\u5173\u7cfb\u4e86.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f17\u6240\u5468\u77e5,\u5728ES6\u4e4b\u524dJS\u4e2d\u7684\u53d8\u91cf\u4f5c\u7528\u57df\u662f\u51fd\u6570\u7ea7\u7684,\u4e0d\u5177\u6709\u5757\u7ea7\u4f5c\u7528\u57df,\u800c\u8fd9\u53e5\u8bdd\u4e5f\u4ec5\u9650\u4e8e\u5bf9Javascript\u8bed &hellip; <a href=\"https:\/\/douhan.li\/?p=60\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Javascript\u53d8\u91cf\u4f5c\u7528\u57df<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/douhan.li\/index.php?rest_route=\/wp\/v2\/posts\/60"}],"collection":[{"href":"https:\/\/douhan.li\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/douhan.li\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/douhan.li\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/douhan.li\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=60"}],"version-history":[{"count":2,"href":"https:\/\/douhan.li\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/douhan.li\/index.php?rest_route=\/wp\/v2\/posts\/60\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/douhan.li\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/douhan.li\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/douhan.li\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}