{"id":324,"date":"2018-12-27T01:25:29","date_gmt":"2018-12-26T17:25:29","guid":{"rendered":"https:\/\/wp.winter-weng.cc\/work\/?p=324"},"modified":"2018-12-27T01:25:29","modified_gmt":"2018-12-26T17:25:29","slug":"ch-6-this-or-that","status":"publish","type":"post","link":"https:\/\/wp.winter-weng.cc\/work\/?p=324","title":{"rendered":"Ch. 6 this or That?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_75 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"\u986f\u793a\/\u96b1\u85cf\u5167\u5bb9\u76ee\u9304\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=324\/#%E7%82%BA%E4%BD%95%E6%98%AF_this%EF%BC%9F\" >\u70ba\u4f55\u662f this\uff1f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=324\/#%E5%AE%B9%E6%98%93%E6%B7%B7%E6%B7%86%E4%B9%8B%E8%99%95\" >\u5bb9\u6613\u6df7\u6dc6\u4e4b\u8655<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=324\/#%E8%87%AA%E8%BA%AB_Itself\" >\u81ea\u8eab (Itself)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=324\/#%E5%85%B6%E7%AF%84%E7%96%87_Its_Scope\" >\u5176\u7bc4\u7587 (Its Scope)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=324\/#this_%E6%98%AF%E4%BB%80%E9%BA%BC\" >this \u662f\u4ec0\u9ebc<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/wp.winter-weng.cc\/work\/?p=324\/#%E8%A4%87%E7%BF%92\" >\u8907\u7fd2<\/a><\/li><\/ul><\/nav><\/div>\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%82%BA%E4%BD%95%E6%98%AF_this%EF%BC%9F\"><\/span><strong>\u70ba\u4f55\u662f this\uff1f<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>Why\uff1f\u4f7f\u7528 this\uff0c\u63d0\u4f9b\u4e00\u500b\u66f4\u512a\u96c5\u7684\u65b9\u5f0f\u4f86\u5be6\u4f5c\u7269\u4ef6\u53c3\u8003\u7684\u50b3\u905e\uff0c\u8b93 API \u7684\u8a2d\u8a08\u66f4\u4e7e\u6de8\u3001\u66f4\u6613\u65bc\u91cd\u8907\u4f7f\u7528\u3002<\/p>\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%AE%B9%E6%98%93%E6%B7%B7%E6%B7%86%E4%B9%8B%E8%99%95\"><\/span><strong>\u5bb9\u6613\u6df7\u6dc6\u4e4b\u8655<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p class=\"has-text-color has-vivid-red-color\">\u5b83\u5176\u5be6\u4e0d\u662f\uff1a\u81ea\u8eab (Itself)\u3001\u5176\u7bc4\u7587 (Its Scope)\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%87%AA%E8%BA%AB_Itself\"><\/span>\u81ea\u8eab (Itself)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function foo(num) {\n\tconsole.log( \"foo: \" + num );\n\t\/\/ keep track of how many times `foo` is called\n\tthis.count++;\n}\nfoo.count = 0;\nvar i;\nfor (i=0; i&lt;10; i++) {\n\tif (i &gt; 5) {\n\t\tfoo( i );\n\t}\n}\n\/\/ foo: 6\n\/\/ foo: 7\n\/\/ foo: 8\n\/\/ foo: 9\n\/\/ how many times was `foo` called?\nconsole.log( foo.count ); \/\/ 0 -- WTF?<\/pre>\n\n\n<p>\u7bc4\u4f8b\u4e2d\u7531\u65bc this \u7684\u6307\u5411\u4e0d\u660e\u78ba\uff0c\u56e0\u6b64\u5be6\u969b\u4e0a\u6307\u5230\u4e86\u5168\u57df\u3002\u5168\u57df\u4e2d\u5efa\u7acb\u4e86 count \u8b8a\u6578\uff0c++\u5f8c\u70ba NaN\u3002<\/p>\n\n\n<p>\u8ff4\u907f this \u7684\u89e3\u6cd5\uff1a<\/p>\n\n\n<ul class=\"wp-block-list\"><li>\u53e6\u5916\u5efa\u7acb\u7269\u4ef6\u4f86\u6301\u6709\u7279\u6027<\/li><li>\u4f7f\u7528\u81ea\u8eab\u8b58\u5225\u5b57 <code>foo<\/code> \u53d6\u4ee3 this<\/li><\/ul>\n\n\n<p>\u6b63\u78ba\u505a\u6cd5\uff1a\u4f7f\u7528 <code>call()<\/code> \u5c07 this \u6307\u5411 <code>foo<\/code> \u672c\u8eab\u3002<code>foo.call (foo, i);<\/code>\u3002<\/p>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%85%B6%E7%AF%84%E7%96%87_Its_Scope\"><\/span>\u5176\u7bc4\u7587 (Its Scope)<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>this \u4e26\u6c92\u6709\u900f\u904e\u4efb\u4f55\u65b9\u5f0f\u53c3\u8003\u5230\u4e00\u500b\u51fd\u5f0f\u7684\u8a9e\u5f59\u7bc4\u7587\u3002<\/p>\n\n\n<p style=\"background-color:#fbdee6\" class=\"has-background\">P.82 \u7a0b\u5f0f\u78bc\u7bc4\u4f8b\u3002<\/p>\n\n\n<p style=\"background-color:#fff8e3\" class=\"has-background\">\u8a66\u8457\u5c07\u7a0b\u5f0f\u78bc\u5c55\u958b\uff0c\u6703\u767c\u73fe Scope \u662f\u5728\u4e00\u6a23\u7684\u5730\u65b9\u3002<\/p>\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"this_%E6%98%AF%E4%BB%80%E9%BA%BC\"><\/span><strong>this \u662f\u4ec0\u9ebc<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<p>this \u662f\u57f7\u884c\u6642\u671f\u7684\u7e6b\u7d50 (runtime binding)\uff0c\u800c\u975e\u7de8\u5beb\u6642\u671f (author-time)\u3002<br \/>this \u7e6b\u7d50\u53d6\u6c7a\u65bc\u51fd\u5f0f\u88ab\u547c\u53eb\u7684\u65b9\u5f0f\u3002<br \/>\u7576\u51fd\u5f0f\u88ab\u8abf\u7528\u6642\uff0c\u6703\u5275\u5efa\u4e00\u500b\u57f7\u884c\u74b0\u5883\uff0c\u4e26\u8a18\u9304\u547c\u53eb\u5806\u758a (call-stack)\uff0c\u6b64\u7d00\u9304\u7684\u7279\u6027\u4e4b\u4e00\u5c31\u662f this \u53c3\u8003\uff0c\u6703\u5728\u57f7\u884c\u904e\u7a0b\u4e2d\u88ab\u4f7f\u7528\u3002<\/p>\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%A4%87%E7%BF%92\"><\/span><strong>\u8907\u7fd2<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n<ul class=\"wp-block-list\"><li>\u8981\u5b78\u597d this\uff0c\u9996\u5148\u5f97\u5b78\u6703 this \u4e0d\u662f\u4ec0\u9ebc\u3002<\/li><li>this \u662f\u51fd\u5f0f\u88ab\u547c\u53eb\u6642\u6240\u9032\u884c\u7684\u4e00\u500b\u7e6b\u7d50\uff0c\u800c\u5b83\u6240\u53c3\u8003\u5230\u7684\u662f\u4ec0\u9ebc\uff0c\u7531\u547c\u53eb\u5730\u9ede (call-site) \u4f86\u6c7a\u5b9a\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u70ba\u4f55\u662f this\uff1f Why\uff1f\u4f7f\u7528 this\uff0c\u63d0\u4f9b\u4e00&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[13,12],"tags":[21],"class_list":["post-324","post","type-post","status-publish","format-standard","hentry","category-jsclosuresthis","category-12","tag-javascript"],"_links":{"self":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=324"}],"version-history":[{"count":0,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=\/wp\/v2\/posts\/324\/revisions"}],"wp:attachment":[{"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.winter-weng.cc\/work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}