{"id":2374,"date":"2024-11-10T16:06:32","date_gmt":"2024-11-10T08:06:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2374\/"},"modified":"2024-11-10T16:06:32","modified_gmt":"2024-11-10T08:06:32","slug":"javascript%e9%97%ad%e5%8c%85%e4%b8%ad%e5%8c%bf%e5%90%8d%e5%87%bd%e6%95%b0%e7%9a%84this%e6%8c%87%e5%90%91%e9%97%ae%e9%a2%98%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88object-getnamefunc%e8%be%93%e5%87%ba","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2374\/","title":{"rendered":"JavaScript\u95ed\u5305\u4e2d\u533f\u540d\u51fd\u6570\u7684`this`\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48`object.getNameFunc()`\u8f93\u51fa\u201cThe Window\u201d\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173072215195274.jpg\" class=\"aligncenter\" title=\"JavaScript\u95ed\u5305\u4e2d\u533f\u540d\u51fd\u6570\u7684`this`\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48`object.getNameFunc()`\u8f93\u51fa\u201cThe Window\u201d\uff1f\u63d2\u56fe\" alt=\"JavaScript\u95ed\u5305\u4e2d\u533f\u540d\u51fd\u6570\u7684`this`\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48`object.getNameFunc()`\u8f93\u51fa\u201cThe Window\u201d\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript\u95ed\u5305\u4e2d\u7684\u95ee\u9898\u89e3\u8bfb<\/strong><\/p>\n<p>\u5728javascript\u4e2d\uff0c\u95ed\u5305\u53ef\u4ee5\u5728\u4e00\u4e2a\u79c1\u6709\u53d8\u91cf\u8303\u56f4\u5185\u8bbf\u95ee\u5c5e\u4e8e\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u51fd\u6570\u5b9e\u73b0\u201c\u5ef6\u8fdf\u6267\u884c\u201d\u6216\u201c\u6570\u636e\u4fdd\u5b58\u201d\u7b49\u76ee\u7684\u3002\u4f46\u6709\u65f6\u5019\u95ed\u5305\u4e2d\u7684\u4f5c\u7528\u57df\u4f1a\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\u3002<\/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><strong>\u95ee\u9898\uff1a<\/strong>\u4e0a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e0c\u671balert\u8f93\u51fa&#8221;my object&#8221;\uff0c\u4f46\u5b9e\u9645\u4e0a\u5374\u8f93\u51fa&#8221;the window&#8221;\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n<p><strong>\u7b54\u6848\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li> <strong>\u533f\u540d\u51fd\u6570\u7684this\uff1a<\/strong>\u533f\u540d\u51fd\u6570\uff08\u5373\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\uff09\u7684this\u4e00\u822c\u6307\u5411window\u3002<\/li>\n<li> <strong>\u95ed\u5305\u7684\u4f5c\u7528\uff1a<\/strong>\u5728\u6b64\u4f8b\u4e2d\uff0cgetnamefunc\u4e2d\u7684\u533f\u540d\u51fd\u6570\u7684this\u6307\u5411window\uff0c\u4f46\u8be5\u51fd\u6570\u901a\u8fc7\u95ed\u5305\u5f15\u7528\u4e86getnamefunc\u7684this\uff0c\u4e5f\u5c31\u662fobject\u3002<\/li>\n<li> <strong>\u4f5c\u7528\u57df\u94fe\uff1a<\/strong>\u533f\u540d\u51fd\u6570\u6267\u884c\u65f6\u5bfb\u627ethat\u53d8\u91cf\uff0c\u9996\u5148\u4f1a\u5728\u81ea\u8eab\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\u4e2d\u67e5\u627e\u4e0d\u5230\u8be5\u53d8\u91cf\uff0c\u968f\u540e\u4f1a\u6cbf\u7740\u4f5c\u7528\u57df\u94fe\u7ee7\u7eed\u5411\u4e0a\u67e5\u627e\u3002<\/li>\n<\/ul>\n<p>\u6b64\u65f6\uff0cthat\u5f15\u7528\u7684\u662fgetnamefunc\u7684this\uff0c\u4e5f\u5c31\u662fobject\uff0c\u6240\u4ee5\u533f\u540d\u51fd\u6570\u4e2dthis.name\u5b9e\u9645\u4e0a\u6307\u5411\u7684\u662fobject.name\uff0c\u800c\u4e0d\u662fwindow.name\u3002<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u8868\u660e\uff0c\u5728\u4f7f\u7528\u95ed\u5305\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u533f\u540d\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\uff0c\u53ef\u80fd\u9700\u8981\u660e\u786e\u6307\u5b9athis\u7684\u6307\u5411\uff0c\u624d\u80fd\u83b7\u5f97\u9884\u671f\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript\u95ed\u5305\u4e2d\u533f\u540d\u51fd\u6570\u7684`this`\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48`object.getNameFunc()`\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\u4e2d\u7684\u95ee\u9898\u89e3\u8bfb \u5728javascript\u4e2d\uff0c\u95ed\u5305\u53ef\u4ee5\u5728\u4e00\u4e2a\u79c1\u6709\u53d8\u91cf\u8303\u56f4\u5185\u8bbf\u95ee\u5c5e\u4e8e\u5916\u90e8\u51fd\u6570\u7684\u53d8\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5957\u51fd\u6570\u5b9e\u73b0\u201c\u5ef6\u8fdf\u6267\u884c\u201d\u6216\u201c\u6570\u636e\u4fdd\u5b58\u201d\u7b49\u76ee\u7684\u3002\u4f46\u6709\u65f6\u5019\u95ed\u5305\u4e2d\u7684\u4f5c\u7528\u57df\u4f1a\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\u3002 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\uff1a\u4e0a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5e0c\u671balert\u8f93\u51fa&#8221;my object&#8221;\uff0c\u4f46\u5b9e\u9645\u4e0a\u5374\u8f93\u51fa&#8221;the window&#8221;\u3002\u8fd9\u662f\u4e3a\u4ec0\u4e48\u5462\uff1f \u7b54\u6848\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u533f\u540d\u51fd\u6570\u7684this\uff1a\u533f\u540d\u51fd\u6570\uff08\u5373\u6ca1\u6709\u540d\u5b57\u7684\u51fd\u6570\uff09\u7684this\u4e00\u822c\u6307\u5411window\u3002 \u95ed\u5305\u7684\u4f5c\u7528\uff1a\u5728\u6b64\u4f8b\u4e2d\uff0cgetnamefunc\u4e2d\u7684\u533f\u540d\u51fd\u6570\u7684this\u6307\u5411window\uff0c\u4f46\u8be5\u51fd\u6570\u901a\u8fc7\u95ed\u5305\u5f15\u7528\u4e86getnamefunc\u7684this\uff0c\u4e5f\u5c31\u662fobject\u3002 \u4f5c\u7528\u57df\u94fe\uff1a\u533f\u540d\u51fd\u6570\u6267\u884c\u65f6\u5bfb\u627ethat\u53d8\u91cf\uff0c\u9996\u5148\u4f1a\u5728\u81ea\u8eab\u7684\u8bcd\u6cd5\u4f5c\u7528\u57df\u4e2d\u67e5\u627e\u4e0d\u5230\u8be5\u53d8\u91cf\uff0c\u968f\u540e\u4f1a\u6cbf\u7740\u4f5c\u7528\u57df\u94fe\u7ee7\u7eed\u5411\u4e0a\u67e5\u627e\u3002 \u6b64\u65f6\uff0cthat\u5f15\u7528\u7684\u662fgetnamefunc\u7684this\uff0c\u4e5f\u5c31\u662fobject\uff0c\u6240\u4ee5\u533f\u540d\u51fd\u6570\u4e2dthis.name\u5b9e\u9645\u4e0a\u6307\u5411\u7684\u662fobject.name\uff0c\u800c\u4e0d\u662fwindow.name\u3002 \u8fd9\u4e2a\u95ee\u9898\u8868\u660e\uff0c\u5728\u4f7f\u7528\u95ed\u5305\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u533f\u540d\u51fd\u6570\u7684\u4f5c\u7528\u57df\u94fe\uff0c\u53ef\u80fd\u9700\u8981\u660e\u786e\u6307\u5b9athis\u7684\u6307\u5411\uff0c\u624d\u80fd\u83b7\u5f97\u9884\u671f\u7684\u7ed3\u679c\u3002 \u4ee5\u4e0a\u5c31\u662fJavaScript\u95ed\u5305\u4e2d\u533f\u540d\u51fd\u6570\u7684`this`\u6307\u5411\u95ee\u9898\uff1a\u4e3a\u4ec0\u4e48`object.getNameFunc()`\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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2374","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2374","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=2374"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2374\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}