{"id":2576,"date":"2024-11-10T14:44:48","date_gmt":"2024-11-10T06:44:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2576\/"},"modified":"2024-11-10T14:44:48","modified_gmt":"2024-11-10T06:44:48","slug":"javascript-%e4%b8%ad%e7%9a%84-this%ef%bc%9a%e7%a9%b6%e7%ab%9f%e6%8c%87%e5%90%91%e5%93%aa%e9%87%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2576\/","title":{"rendered":"JavaScript \u4e2d\u7684 this\uff1a\u7a76\u7adf\u6307\u5411\u54ea\u91cc\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173080101646655.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u7684 this\uff1a\u7a76\u7adf\u6307\u5411\u54ea\u91cc\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u7684 this\uff1a\u7a76\u7adf\u6307\u5411\u54ea\u91cc\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6df1\u5165\u4e86\u89e3 js \u4e2d this \u7684\u7528\u6cd5<\/strong><\/p>\n<p>\u867d\u7136\u6587\u7ae0\u63d0\u5230 this \u7684\u503c\u4f1a\u6839\u636e\u51fd\u6570\u8c03\u7528\u65b9\u5f0f\u800c\u53d8\u5316\uff0c\u4f46\u5b83\u6709\u4e00\u4e2a\u6052\u5b9a\u7684\u539f\u5219\uff1athis \u59cb\u7ec8\u6307\u5411\u8c03\u7528\u51fd\u6570\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u4e86\u89e3 this \u7684\u7528\u6cd5\uff0c\u8bf7\u7ee7\u7eed\u7ee7\u7eed\u9605\u8bfb\u3002<\/p>\n<p><strong>this \u7684\u5e38\u89c1\u7528\u6cd5<\/strong><\/p>\n<ul>\n<li> <strong>\u65b9\u6cd5\u8c03\u7528\uff1a<\/strong>\u51fd\u6570\u4f5c\u4e3a\u67d0\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0cthis \u6307\u5411\u8be5\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>const person = {\n  name: \"john\",\n  greet: function() {\n    console.log(`hello, my name is ${this.name}.`);\n  }\n};\n\nperson.greet(); \/\/ \u8f93\u51fa\uff1ahello, my name is john.<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff1a<\/strong>\u51fd\u6570\u4f5c\u4e3a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u88ab\u8c03\u7528\u65f6\uff0cthis \u6307\u5411\u89e6\u53d1\u4e8b\u4ef6\u7684 dom \u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>const button = document.getelementbyid(\"mybutton\");\n\nbutton.addeventlistener(\"click\", function() {\n  console.log(this); \/\/ \u8f93\u51fa\uff1a&lt;!\u2014 dom element 'mybutton' \u2014&gt;\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>\u7bad\u5934\u51fd\u6570\uff1a<\/strong>\u7bad\u5934\u51fd\u6570\uff08=&gt;\uff09\u5185\u90e8\u6ca1\u6709\u81ea\u5df1\u7684 this\uff0c\u5b83\u4f1a\u7ee7\u627f\u7236\u4e2d\u7684 this\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>const obj = {\n  name: \"jane\",\n  greet: () =&gt; {\n    console.log(`hello, my name is ${this.name}.`);\n  }\n};\n\nobj.greet(); \/\/ \u8f93\u51fa\uff1ahello, my name is undefined.<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7279\u6b8a\u60c5\u51b5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li> <strong>bind() \u65b9\u6cd5\uff1a<\/strong>\u53ef\u4ee5\u624b\u52a8\u5c06\u7279\u5b9a\u7684\u5bf9\u8c61\u7ed1\u5b9a\u4e3a\u51fd\u6570\u7684 this\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>const greeting = function() {\n  console.log(`Hello, my name is ${this.name}.`);\n};\n\nconst boundGreeting = greeting.bind({ name: \"Tom\" });\n\nboundGreeting(); \/\/ \u8f93\u51fa\uff1aHello, my name is Tom.<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>apply() \u548c call() \u65b9\u6cd5\uff1a<\/strong>\u7c7b\u4f3c\u4e8e bind() \u65b9\u6cd5\uff0c\u5b83\u4eec\u53ef\u4ee5\u624b\u52a8\u8bbe\u7f6e this \u7684\u503c\u3002apply() \u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u6570\u7ec4\uff0c\u800c call() \u63a5\u53d7\u5355\u4e2a\u53c2\u6570\u5217\u8868\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u7528\u6cd5\u53ea\u662f this \u5728 javascript \u4e2d\u4f17\u591a\u7528\u6cd5\u4e2d\u7684\u4e00\u5c0f\u90e8\u5206\u3002\u7406\u89e3 this \u7684\u52a8\u6001\u7279\u6027\u5bf9\u4e8e\u7f16\u5199\u5e72\u51c0\u3001\u53ef\u7ef4\u62a4\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d\u7684 this\uff1a\u7a76\u7adf\u6307\u5411\u54ea\u91cc\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>\u6df1\u5165\u4e86\u89e3 js \u4e2d this \u7684\u7528\u6cd5 \u867d\u7136\u6587\u7ae0\u63d0\u5230 this \u7684\u503c\u4f1a\u6839\u636e\u51fd\u6570\u8c03\u7528\u65b9\u5f0f\u800c\u53d8\u5316\uff0c\u4f46\u5b83\u6709\u4e00\u4e2a\u6052\u5b9a\u7684\u539f\u5219\uff1athis \u59cb\u7ec8\u6307\u5411\u8c03\u7528\u51fd\u6570\u7684\u5bf9\u8c61\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u4e86\u89e3 this \u7684\u7528\u6cd5\uff0c\u8bf7\u7ee7\u7eed\u7ee7\u7eed\u9605\u8bfb\u3002 this \u7684\u5e38\u89c1\u7528\u6cd5 \u65b9\u6cd5\u8c03\u7528\uff1a\u51fd\u6570\u4f5c\u4e3a\u67d0\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0cthis \u6307\u5411\u8be5\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a const person = { name: &#8220;john&#8221;, greet: function() { console.log(`hello, my name is ${this.name}.`); } }; person.greet(); \/\/ \u8f93\u51fa\uff1ahello, my name is john. \u767b\u5f55\u540e\u590d\u5236 \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\uff1a\u51fd\u6570\u4f5c\u4e3a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u88ab\u8c03\u7528\u65f6\uff0cthis \u6307\u5411\u89e6\u53d1\u4e8b\u4ef6\u7684 dom \u5143\u7d20\u3002\u4f8b\u5982\uff1a const button = document.getelementbyid(&#8220;mybutton&#8221;); button.addeventlistener(&#8220;click&#8221;, function() { console.log(this); \/\/ \u8f93\u51fa\uff1a&lt;!\u2014 dom element [&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-2576","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2576","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=2576"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2576\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}