{"id":2300,"date":"2024-11-10T17:23:55","date_gmt":"2024-11-10T09:23:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2300\/"},"modified":"2024-11-10T17:23:55","modified_gmt":"2024-11-10T09:23:55","slug":"javascript-%e9%97%ad%e5%8c%85%e4%b8%adthis%e6%8c%87%e5%90%91%e9%97%ae%e9%a2%98%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88alertobject-getnamefunc-%e4%bc%9a%e8%be%93%e5%87%bathe-window","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2300\/","title":{"rendered":"JavaScript \u95ed\u5305\u4e2dthis\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48alert(object.getNameFunc()()) \u4f1a\u8f93\u51fa\u201cThe Window\u201d\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173071080418539.jpg\" class=\"aligncenter\" title=\"JavaScript \u95ed\u5305\u4e2dthis\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48alert(object.getNameFunc()()) \u4f1a\u8f93\u51fa\u201cThe Window\u201d\uff1f\u63d2\u56fe\" alt=\"JavaScript \u95ed\u5305\u4e2dthis\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48alert(object.getNameFunc()()) \u4f1a\u8f93\u51fa\u201cThe Window\u201d\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u95ed\u5305\u7591\u96be\u5256\u6790<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0c\u95ed\u5305\u53ef\u4ee5\u8d4b\u4e88\u5185\u5c42\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\u7684\u6743\u529b\uff0c\u4f46\u6709\u65f6\u4e5f\u4f1a\u5f15\u53d1\u4f5c\u7528\u57df\u95ee\u9898\u3002\u8ba9\u6211\u4eec\u4ee5\u4e00\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e3a\u4f8b\u6765\u63a2\u8ba8\u4e00\u4e2a\u5e38\u89c1\u7684\u95ed\u5305\u4f5c\u7528\u57df\u95ee\u9898\uff1a<\/p>\n<pre>var name = \"The Window\";\nvar object = {\n  name: \"My Object\",\n  getNameFunc: function() {\n    return function() {\n      return this.name;\n    };\n  }\n};\nalert(object.getNameFunc()());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u95ee\u9898\u662f\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u7684 alert() \u4f1a\u8f93\u51fa\u4ec0\u4e48\u503c\uff1f<\/p>\n<p>\u7b54\u6848\u662f &#8220;the window&#8221;\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u533f\u540d\u51fd\u6570\u7684\u884c\u4e3a\u662f\u7531 javascript \u4e2d\u7684 &#8220;this&#8221; \u5173\u952e\u5b57\u51b3\u5b9a\u7684\u3002\u533f\u540d\u51fd\u6570\u7684 &#8220;this&#8221; \u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5168\u5c40\u5bf9\u8c61 window\u3002\u7136\u800c\uff0c\u5728\u95ed\u5305\u7684\u60c5\u51b5\u4e0b\uff0c\u533f\u540d\u51fd\u6570\u7684 &#8220;this&#8221; \u53ef\u4ee5\u5f15\u7528\u5916\u90e8\u51fd\u6570\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0cgetnamefunc \u51fd\u6570\u7684 this \u6307\u5411\u5bf9\u8c61 object\u3002\u4f46\u662f\uff0cgetnamefunc \u8fd4\u56de\u7684\u533f\u540d\u51fd\u6570\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u51fd\u6570\uff0c\u5176 this \u6307\u5411\u5168\u5c40\u5bf9\u8c61 window\u3002<\/p>\n<p>\u56e0\u6b64\uff0cobject.getnamefunc()() \u8fd4\u56de\u7684\u533f\u540d\u51fd\u6570\u7684 this \u6307\u5411 window\uff0c\u800c\u4e0d\u662f\u5bf9\u8c61 object\u3002\u7531\u4e8e name \u5c5e\u6027\u5b58\u5728\u4e8e window \u5bf9\u8c61\u4e2d\uff0c\u56e0\u6b64\u533f\u540d\u51fd\u6570\u8fd4\u56de window.name\uff0c\u5373 &#8220;the window&#8221;\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u95ed\u5305\u4e2dthis\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48alert(object.getNameFunc()()) \u4f1a\u8f93\u51fa\u201cThe Window\u201d\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u95ed\u5305\u7591\u96be\u5256\u6790 \u5728 javascript \u4e2d\uff0c\u95ed\u5305\u53ef\u4ee5\u8d4b\u4e88\u5185\u5c42\u51fd\u6570\u8bbf\u95ee\u5916\u90e8\u53d8\u91cf\u7684\u6743\u529b\uff0c\u4f46\u6709\u65f6\u4e5f\u4f1a\u5f15\u53d1\u4f5c\u7528\u57df\u95ee\u9898\u3002\u8ba9\u6211\u4eec\u4ee5\u4e00\u4e2a\u4ee3\u7801\u793a\u4f8b\u4e3a\u4f8b\u6765\u63a2\u8ba8\u4e00\u4e2a\u5e38\u89c1\u7684\u95ed\u5305\u4f5c\u7528\u57df\u95ee\u9898\uff1a var name = &#8220;The Window&#8221;; var object = { name: &#8220;My Object&#8221;, getNameFunc: function() { return function() { return this.name; }; } }; alert(object.getNameFunc()()); \u767b\u5f55\u540e\u590d\u5236 \u95ee\u9898\u662f\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4e2d\u7684 alert() \u4f1a\u8f93\u51fa\u4ec0\u4e48\u503c\uff1f \u7b54\u6848\u662f &#8220;the window&#8221;\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u533f\u540d\u51fd\u6570\u7684\u884c\u4e3a\u662f\u7531 javascript \u4e2d\u7684 &#8220;this&#8221; \u5173\u952e\u5b57\u51b3\u5b9a\u7684\u3002\u533f\u540d\u51fd\u6570\u7684 &#8220;this&#8221; \u901a\u5e38\u88ab\u8ba4\u4e3a\u662f\u5168\u5c40\u5bf9\u8c61 window\u3002\u7136\u800c\uff0c\u5728\u95ed\u5305\u7684\u60c5\u51b5\u4e0b\uff0c\u533f\u540d\u51fd\u6570\u7684 &#8220;this&#8221; \u53ef\u4ee5\u5f15\u7528\u5916\u90e8\u51fd\u6570\u7684\u5bf9\u8c61\u3002 \u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0cgetnamefunc \u51fd\u6570\u7684 this \u6307\u5411\u5bf9\u8c61 object\u3002\u4f46\u662f\uff0cgetnamefunc \u8fd4\u56de\u7684\u533f\u540d\u51fd\u6570\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u51fd\u6570\uff0c\u5176 this \u6307\u5411\u5168\u5c40\u5bf9\u8c61 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2300","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2300"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2300\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}