{"id":2617,"date":"2024-11-10T17:43:43","date_gmt":"2024-11-10T09:43:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2617\/"},"modified":"2024-11-10T17:43:43","modified_gmt":"2024-11-10T09:43:43","slug":"js-%e4%b8%ad-this-%e6%8c%87%e5%90%91%e7%9a%84%e5%9b%b0%e6%83%91%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e8%bf%99%e4%b8%a4%e7%a7%8d%e4%bb%a3%e7%a0%81%e6%ae%b5%e7%9a%84-this-%e9%83%bd%e6%8c%87%e5%90%91-wind","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2617\/","title":{"rendered":"JS \u4e2d this \u6307\u5411\u7684\u56f0\u60d1\uff1a\u4e3a\u4ec0\u4e48\u8fd9\u4e24\u79cd\u4ee3\u7801\u6bb5\u7684 this \u90fd\u6307\u5411 window\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173081455067921.jpg\" class=\"aligncenter\" title=\"JS \u4e2d this \u6307\u5411\u7684\u56f0\u60d1\uff1a\u4e3a\u4ec0\u4e48\u8fd9\u4e24\u79cd\u4ee3\u7801\u6bb5\u7684 this \u90fd\u6307\u5411 window\uff1f\u63d2\u56fe\" alt=\"JS \u4e2d this \u6307\u5411\u7684\u56f0\u60d1\uff1a\u4e3a\u4ec0\u4e48\u8fd9\u4e24\u79cd\u4ee3\u7801\u6bb5\u7684 this \u90fd\u6307\u5411 window\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>js \u4e2d\u4e00\u4e2a\u5173\u4e8e this \u6307\u5411\u7684\u4e0d\u89e3\u4e4b\u8c1c<\/strong><\/p>\n<p>\u5728\u63a2\u8ba8 this \u6307\u5411\u7684\u590d\u6742\u6027\u65f6\uff0c\u6211\u4eec\u9047\u5230\u4e86\u4e00\u4e9b\u65b0\u95ee\u9898\uff0c\u8ba9\u6211\u4eec\u5c1d\u8bd5\u9010\u4e00\u89e3\u51b3\u3002<\/p>\n<p><strong>\u95ee\u9898 1:<\/strong><br \/>\u7b2c 2 \u4e2a\u4ee3\u7801\u6bb5\u7b49\u4ef7\u4e8e\u7b2c 3 \u4e2a\u4ee3\u7801\u6bb5\u5417\uff1f<\/p>\n<pre>function foo() {\n    console.log(this)\n}\n\nvar obj = {\n    a: 2,\n    foo: foo\n}\nobj.foo()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u95ee\u9898 2:<\/strong><br \/>\u4e3a\u4ec0\u4e48\u7b2c 2 \u4e2a\u548c\u7b2c 3 \u4e2a\u4ee3\u7801\u6bb5\u4e2d this \u90fd\u6307\u5411 window\uff1f<\/p>\n<pre>function foo() {\n    console.log(this)\n}\n\nvar obj = {\n    a: 2,\n    foo: foo() \/\/ \u7b2c 2 \u4e2a\u4ee3\u7801\u6bb5\n}\nvar obj = {\n    a: 2,\n    foo: console.log(this) \/\/ \u7b2c 3 \u4e2a\u4ee3\u7801\u6bb5\n}\nobj.foo()<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7b54\u6848:<\/strong><br \/><strong>\u95ee\u9898 1:<\/strong><br \/>\u4e0d\uff0c\u4e24\u4e2a\u4ee3\u7801\u6bb5\u4e0d\u76f8\u7b49\u3002\u7b2c 2 \u4e2a\u4ee3\u7801\u6bb5\u4e2d\uff0c\u5c06\u62ec\u53f7\u6dfb\u52a0\u5230 foo() \u540e\u8868\u660e\u8981\u6267\u884c\u8be5\u65b9\u6cd5\u3002\u8fd9\u6837\u505a\u4f1a\u5c06\u65b9\u6cd5\u8fd4\u56de\u7684\u7ed3\u679c\u8d4b\u503c\u7ed9 foo \u5c5e\u6027\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u7b2c 3 \u4e2a\u4ee3\u7801\u6bb5\u76f4\u63a5\u5c06 console.log(this) \u7684\u6267\u884c\u7ed3\u679c\u8d4b\u503c\u7ed9 foo \u5c5e\u6027\u3002<\/p>\n<p><strong>\u95ee\u9898 2:<\/strong><br \/>thist \u6307\u5411 window \u7684\u539f\u56e0\u662f\uff1a<\/p>\n<ul>\n<li>\u5728\u7b2c 2 \u4e2a\u4ee3\u7801\u6bb5\u4e2d\uff0cfoo() \u65b9\u6cd5\u5728\u6ca1\u6709\u6307\u5b9a\u8c03\u7528\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\u88ab\u6267\u884c\uff0c\u5bfc\u81f4 this \u9ed8\u8ba4\u6307\u5411 window\u3002<\/li>\n<li>\u5728\u7b2c 3 \u4e2a\u4ee3\u7801\u6bb5\u4e2d\uff0cconsole.log(this) \u7684\u6267\u884c\u7ed3\u679c\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5728 window \u4e0a\u4e0b\u6587\u4e2d\u8c03\u7528\uff0c\u56e0\u6b64 this \u6307\u5411 window\u3002<\/li>\n<\/ul>\n<p><strong>this \u6307\u5411\u7684\u89c4\u5219:<\/strong><\/p>\n<ul>\n<li>this \u6307\u5411\u6700\u8fd1\u8c03\u7528\u7684\u51fd\u6570\u7684\u8c03\u7528\u5bf9\u8c61\u3002<\/li>\n<li>\u5982\u679c\u51fd\u6570\u6ca1\u6709\u88ab\u8c03\u7528\u6216\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u4f7f\u7528\uff0c\u5219 this \u6307\u5411 window\uff08\u5728\u6d4f\u89c8\u5668\u73af\u5883\u4e2d\uff09\u3002<\/li>\n<li>\u5f53\u4f7f\u7528 obj.foo() \u8c03\u7528\u65b9\u6cd5\u65f6\uff0cthis \u6307\u5411 obj \u5bf9\u8c61\u3002<\/li>\n<li>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u5bf9\u8c61\u4e14\u6ca1\u6709\u88ab\u8c03\u7528\uff0c\u5219 this \u4e3a undefined\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fJS \u4e2d this \u6307\u5411\u7684\u56f0\u60d1\uff1a\u4e3a\u4ec0\u4e48\u8fd9\u4e24\u79cd\u4ee3\u7801\u6bb5\u7684 this \u90fd\u6307\u5411 window\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>js \u4e2d\u4e00\u4e2a\u5173\u4e8e this \u6307\u5411\u7684\u4e0d\u89e3\u4e4b\u8c1c \u5728\u63a2\u8ba8 this \u6307\u5411\u7684\u590d\u6742\u6027\u65f6\uff0c\u6211\u4eec\u9047\u5230\u4e86\u4e00\u4e9b\u65b0\u95ee\u9898\uff0c\u8ba9\u6211\u4eec\u5c1d\u8bd5\u9010\u4e00\u89e3\u51b3\u3002 \u95ee\u9898 1:\u7b2c 2 \u4e2a\u4ee3\u7801\u6bb5\u7b49\u4ef7\u4e8e\u7b2c 3 \u4e2a\u4ee3\u7801\u6bb5\u5417\uff1f function foo() { console.log(this) } var obj = { a: 2, foo: foo } obj.foo() \u767b\u5f55\u540e\u590d\u5236 \u95ee\u9898 2:\u4e3a\u4ec0\u4e48\u7b2c 2 \u4e2a\u548c\u7b2c 3 \u4e2a\u4ee3\u7801\u6bb5\u4e2d this \u90fd\u6307\u5411 window\uff1f function foo() { console.log(this) } var obj = { a: 2, foo: foo() \/\/ \u7b2c 2 \u4e2a\u4ee3\u7801\u6bb5 [&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-2617","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2617","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=2617"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2617\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}