{"id":3212,"date":"2024-11-10T08:19:26","date_gmt":"2024-11-10T00:19:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3212\/"},"modified":"2024-11-10T08:19:26","modified_gmt":"2024-11-10T00:19:26","slug":"js%e4%b8%adthis%e5%a6%82%e4%bd%95%e4%bc%a0%e9%80%92","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3212\/","title":{"rendered":"js\u4e2dthis\u5982\u4f55\u4f20\u9012"},"content":{"rendered":"<blockquote><p>\n  \u5728 javascript \u4e2d\u4f20\u9012 this \u5173\u952e\u5b57\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u9690\u5f0f\u4f20\u9012\uff1a\u5f53\u5728\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e2d\u8c03\u7528\u51fd\u6570\u65f6\uff0cthis \u81ea\u52a8\u6307\u5411\u8be5\u5bf9\u8c61\u3002\u663e\u5f0f\u4f20\u9012\uff1a\u4f7f\u7528 call()\u3001apply() \u6216 bind() \u65b9\u6cd5\u53ef\u4ee5\u663e\u5f0f\u4f20\u9012 this\u3002call() \u548c apply() \u5141\u8bb8\u7ed1\u5b9a this \u5230\u6307\u5b9a\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002bind() \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5176\u4e2d this \u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\uff0c\u9690\u5f0f\u4f20\u9012\u6700\u65b9\u4fbf\uff0c\u663e\u5f0f\u4f20\u9012\u53ef\u4ee5\u4f20\u9012 this \u7ed9\u4efb\u4f55\u51fd\u6570\uff0cbind() \u53ef\u4ee5\u521b\u5efa this \u59cb\u7ec8\u7ed1\u5b9a\u5230\u7279\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110807160054397.jpg\" class=\"aligncenter\" title=\"js\u4e2dthis\u5982\u4f55\u4f20\u9012\u63d2\u56fe\" alt=\"js\u4e2dthis\u5982\u4f55\u4f20\u9012\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 JavaScript \u4e2d\u4f20\u9012 this<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0cthis \u5173\u952e\u5b57\u6307\u5411\u6b63\u5728\u6267\u884c\u4ee3\u7801\u7684\u5bf9\u8c61\u3002\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u7ed1\u5b9a\u503c\uff0c\u5728\u51fd\u6570\u6267\u884c\u65f6\u786e\u5b9a\u3002<\/p>\n<p><strong>\u4f20\u9012 this \u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f20\u9012 this\uff1a<\/p>\n<ul>\n<li> <strong>\u9690\u5f0f\u4f20\u9012\uff1a<\/strong>\u5f53\u5728\u5bf9\u8c61\u7684\u65b9\u6cd5\u5185\u90e8\u8c03\u7528\u51fd\u6570\u65f6\uff0cthis \u4f1a\u81ea\u52a8\u6307\u5411\u8be5\u5bf9\u8c61\u3002\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const obj = {\n  name: 'John',\n  getName: function() {\n    return this.name;\n  }\n};\n\nobj.getName(); \/\/ \"John\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li> <strong>\u663e\u5f0f\u4f20\u9012\uff1a<\/strong>\u4f7f\u7528 call()\u3001apply() \u6216 bind() \u65b9\u6cd5\u53ef\u4ee5\u663e\u5f0f\u5730\u4f20\u9012 this\u3002<\/li>\n<\/ul>\n<p><strong>call() \u548c apply()\uff1a<\/strong><\/p>\n<ul>\n<li>call() \u548c apply() \u5141\u8bb8\u60a8\u5c06 this \u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/li>\n<li>call() \u63a5\u53d7\u5355\u4e2a\u53c2\u6570\u5217\u8868\uff0c\u800c apply() \u63a5\u53d7\u6570\u7ec4\u5f62\u5f0f\u7684\u53c2\u6570\u5217\u8868\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const obj1 = { name: 'John' };\nconst obj2 = { name: 'Jane' };\n\nfunction getName() {\n  return this.name;\n}\n\ngetName.call(obj1); \/\/ \"John\"\ngetName.apply(obj2); \/\/ \"Jane\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>bind()\uff1a<\/strong><\/p>\n<ul>\n<li>bind() \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5176\u4e2d this \u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002<\/li>\n<li>\u65b0\u51fd\u6570\u53ef\u4ee5\u4e0e\u4efb\u4f55\u53c2\u6570\u5217\u8868\u4e00\u8d77\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const obj = { name: 'John' };\n\nconst getNameBound = getName.bind(obj);\n\ngetNameBound(); \/\/ \"John\"\ngetNameBound('Jane'); \/\/ \"John\" (\u56e0\u4e3a `this` \u5df2\u7ed1\u5b9a\u5230 `obj`)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\u4f20\u9012 this \u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\uff1a<\/p>\n<ul>\n<li>\u9690\u5f0f\u4f20\u9012\u6700\u65b9\u4fbf\uff0c\u4f46\u53ea\u9002\u7528\u4e8e\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u663e\u5f0f\u4f20\u9012\u63d0\u4f9b\u4e86\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u60a8\u4f20\u9012 this \u7ed9\u4efb\u4f55\u51fd\u6570\u3002<\/li>\n<li>bind() \u5f88\u6709\u7528\uff0c\u56e0\u4e3a\u53ef\u4ee5\u521b\u5efa\u65b0\u7684\u51fd\u6570\uff0c\u5176\u4e2d this \u59cb\u7ec8\u7ed1\u5b9a\u5230\u7279\u5b9a\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u4e2dthis\u5982\u4f55\u4f20\u9012\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>\u5728 javascript \u4e2d\u4f20\u9012 this \u5173\u952e\u5b57\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u9690\u5f0f\u4f20\u9012\uff1a\u5f53\u5728\u5bf9\u8c61\u7684\u65b9\u6cd5\u4e2d\u8c03\u7528\u51fd\u6570\u65f6\uff0cthis \u81ea\u52a8\u6307\u5411\u8be5\u5bf9\u8c61\u3002\u663e\u5f0f\u4f20\u9012\uff1a\u4f7f\u7528 call()\u3001apply() \u6216 bind() \u65b9\u6cd5\u53ef\u4ee5\u663e\u5f0f\u4f20\u9012 this\u3002call() \u548c apply() \u5141\u8bb8\u7ed1\u5b9a this \u5230\u6307\u5b9a\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002bind() \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u5176\u4e2d this \u7ed1\u5b9a\u5230\u6307\u5b9a\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u60c5\u51b5\uff0c\u9690\u5f0f\u4f20\u9012\u6700\u65b9\u4fbf\uff0c\u663e\u5f0f\u4f20\u9012\u53ef\u4ee5\u4f20\u9012 this \u7ed9\u4efb\u4f55\u51fd\u6570\uff0cbind() \u53ef\u4ee5\u521b\u5efa this \u59cb\u7ec8\u7ed1\u5b9a\u5230\u7279 \u5728 JavaScript \u4e2d\u4f20\u9012 this \u5728 JavaScript \u4e2d\uff0cthis \u5173\u952e\u5b57\u6307\u5411\u6b63\u5728\u6267\u884c\u4ee3\u7801\u7684\u5bf9\u8c61\u3002\u5b83\u662f\u4e00\u4e2a\u52a8\u6001\u7ed1\u5b9a\u503c\uff0c\u5728\u51fd\u6570\u6267\u884c\u65f6\u786e\u5b9a\u3002 \u4f20\u9012 this \u7684\u65b9\u6cd5 \u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f20\u9012 this\uff1a \u9690\u5f0f\u4f20\u9012\uff1a\u5f53\u5728\u5bf9\u8c61\u7684\u65b9\u6cd5\u5185\u90e8\u8c03\u7528\u51fd\u6570\u65f6\uff0cthis \u4f1a\u81ea\u52a8\u6307\u5411\u8be5\u5bf9\u8c61\u3002\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002 \u4f8b\u5982\uff1a const obj = { name: &#8216;John&#8217;, getName: function() { return this.name; } }; obj.getName(); \/\/ [&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-3212","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3212","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=3212"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3212\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}