{"id":1398,"date":"2022-07-10T23:08:46","date_gmt":"2022-07-10T15:08:46","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1398"},"modified":"2022-07-14T13:08:47","modified_gmt":"2022-07-14T05:08:47","slug":"28-jian-tou-han-shu-he-this-guan-jian-zi","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1398","title":{"rendered":"28 &#8211; \u7bad\u5934\u51fd\u6570\u548c this \u5173\u952e\u5b57"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/07\/16574657594332.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/arrow-functions-this-keyword-350j\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/arrow-functions-this-keyword-350j<\/a><\/p>\n<p>\u5728\u4e4b\u524d\u7684\u535a\u6587\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u5b66\u8fc7\u4e86\u7bad\u5934\u51fd\u6570\u548c this \u5173\u952e\u5b57\u3002\u73b0\u5728\u6211\u4eec\u5c06\u628a\u5b83\u4fe9\u7ed3\u5408\u8d77\u6765\u5e76\u4e14\u770b\u770b\u7bad\u5934\u51fd\u6570\u4e0e\u6807\u51c6\u7684\u51fd\u6570\u8868\u8fbe\u5f0f\u884c\u4e3a\u4e0a\u6709\u4f55\u4e0d\u540c\u3002<\/p>\n<p>\u7bad\u5934\u51fd\u6570\uff0c\u591a\u6570\u65f6\u5019\u50cf\u51fd\u6570\u8868\u8fbe\u5f0f\u7684\u7b80\u5199\u8bed\u6cd5\u3002\u6700\u91cd\u8981\u7684\u533a\u522b\u662f\u7bad\u5934\u51fd\u6570\u6ca1\u6709\u548c <code>this<\/code> \u5173\u952e\u5b57\u7ed1\u5b9a\u3002<\/p>\n<p>\u5bf9\u4e00\u4e2a\u51fd\u6570\u8868\u8fbe\u5f0f\u6765\u8bf4\uff0c<code>this<\/code> \u4f1a\u968f\u7740\u51fd\u6570\u8c03\u7528\u65f6\u7684\u4e0a\u4e0b\u6587\u6539\u53d8\u3002\u800c\u7bad\u5934\u51fd\u6570\uff0c<code>this<\/code> \u4f1a\u57fa\u4e8e\u8bcd\u6cd5\u4f5c\u7528\u57df\u3002\u7bad\u5934\u51fd\u6570\u9075\u5faa\u666e\u901a\u53d8\u91cf\u7684\u5bfb\u627e\u65b9\u5f0f\uff0c\u5148\u5728\u5f53\u524d\u4f5c\u7528\u57df\u4e2d\u67e5\u627e <code>this<\/code> \uff0c\u82e5\u6ca1\u6709\u627e\u5230\u5219\u4f1a\u7ee7\u7eed\u5bfb\u627e\u4e0a\u4e00\u5c42\u4f5c\u7528\u57df\u3002<\/p>\n<p>\u6211\u4eec\u5c06\u4f1a\u4f7f\u7528\u4e0a\u4e00\u7bc7\u6587\u7ae0\u7684\u573a\u666f\uff1a<\/p>\n<ol>\n<li>\u76f4\u63a5\u8c03\u7528\u3002<\/li>\n<li>\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\u3002<\/li>\n<li>\u4f5c\u4e3a\u5bf9\u8c61\u65b9\u6cd5\u8c03\u7528\u3002<\/li>\n<li>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u4f7f\u7528\u3002<\/li>\n<li>\u5728\u4e8b\u4ef6\u4e2d\u4f7f\u7528\u3002<\/li>\n<\/ol>\n<h2><a id=\"%E7%9B%B4%E6%8E%A5%E8%B0%83%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u76f4\u63a5\u8c03\u7528<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const foo = () =&gt; {\n    return this;\n}\n\nconsole.log(foo()); \/\/ window or global object\n<\/code><\/pre>\n<p>\u5b8c\u5168\u4e0e\u51fd\u6570\u8868\u8fbe\u5f0f\u76f8\u540c\u3002<\/p>\n<h2><a id=\"%E4%BD%9C%E4%B8%BA%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f5c\u4e3a\u6784\u9020\u51fd\u6570<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const Order = (main, side, dessert) =&gt; {\n    this.main = main;\n    this.side = side;\n    this.dessert = dessert;\n    this.order = function () {\n        return `I will have ${this.main} with ${this.side} and finish off with a ${this.dessert}`;\n    }\n}\nconst newOrder = new Order(&quot;sushi&quot;, &quot;soup&quot;, &quot;yogurt&quot;); \/\/ Order is not a constructor\n\nconsole.log(newOrder.order());\n<\/code><\/pre>\n<p><strong>\u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u7528\u4f5c\u6784\u9020\u5668<\/strong>\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u4ed6\u4eec\u8868\u73b0\u7684\u4e0d\u540c\uff0c\u867d\u7136\u5c06this.order \u6539\u4e3a\u7bad\u5934\u51fd\u6570\uff0c\u4f46\u5982\u679c\u6211\u4eec\u4e0d\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\uff0c\u6548\u679c\u4e5f\u662f\u4e00\u6837\u7684\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function Order(main, side, dessert) {\n    this.main = main;\n    this.side = side;\n    this.dessert = dessert;\n    this.order = () =&gt; {\n        return `I will have ${ this.main } with ${ this.side } and finish off with a ${ this.dessert } `;\n    }\n}\nconst newOrder = new Order(&quot;sushi&quot;, &quot;soup&quot;, &quot;yogurt&quot;);\n\nconsole.log(newOrder.order());\n\/\/ I will have sushi with soup and finish off with a yogurt\n<\/code><\/pre>\n<h2><a id=\"%E4%BD%9C%E4%B8%BA%E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E8%B0%83%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f5c\u4e3a\u5bf9\u8c61\u65b9\u6cd5\u8c03\u7528<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const myObject = {\n    main: &quot;butter chicken&quot;,\n    side: &quot;rice&quot;,\n    dessert: &quot;ice cream&quot;,\n    order: () =&gt; {\n        return `I will have ${this.main} with ${this.side} and finish off with ${this.dessert}`;\n    }\n}\n\nconsole.log(myObject.order());\n\/\/ I will have undefined with undefined and finish off with undefined\n<\/code><\/pre>\n<p><strong>\u4e0e\u51fd\u6570\u8868\u8fbe\u5f0f\u8868\u73b0\u7684\u4e0d\u540c<\/strong>\uff0c \u4f5c\u4e3a\u5bf9\u8c61\u65b9\u6cd5\u65f6\u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u76f4\u63a5\u66ff\u6362\u51fd\u6570\u8868\u8fbe\u5f0f\u3002<\/p>\n<p>\u4e3a\u4f55 <code>main<\/code>\u3001<code>side<\/code> \u548c <code>dessert<\/code> \u7684\u503c\u4e3a undefined ? \u7bad\u5934\u51fd\u6570\u5185\u90e8\u7684 <code>this<\/code> \u6307\u5411\u6211\u4eec\u5b9a\u4e49\u5bf9\u8c61 <code>myObject<\/code> \u65f6\u73af\u5883\uff08\u5728\u8fd9\u91cc\u6307\u5411 window\uff09\u3002window \u5bf9\u8c61\u4e0a\u4e0d\u5305\u542b order \u9700\u8981\u7684\u4e09\u4e2a\u53d8\u91cf\u3002<\/p>\n<h2><a id=\"%E5%9C%A8%E4%B8%A5%E6%A0%BC%E6%A8%A1%E5%BC%8F%E4%B8%8B%E4%BD%BF%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\u4f7f\u7528<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">&quot;use strict&quot;;\nconst foo = () =&gt; {\n    return this;\n};\n\nconsole.log(foo() === undefined); \/\/ false\nconsole.log(foo()); \/\/ window or global object\n<\/code><\/pre>\n<p><strong>\u4e0e\u51fd\u6570\u8868\u8fbe\u5f0f\u8868\u73b0\u4e0d\u540c<\/strong>\uff0c\u56e0\u4e3a\u8bcd\u6cd5\u4f5c\u7528\u57df\u4e2d\u7684 this \u4f18\u5148\u4e0e\u4e25\u683c\u6a21\u5f0f\u4e0b\u7684 this \u89c4\u5219\u3002<\/p>\n<h2><a id=\"%E5%9C%A8%E4%BA%8B%E4%BB%B6%E4%B8%AD%E4%BD%BF%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5728\u4e8b\u4ef6\u4e2d\u4f7f\u7528<\/h2>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta content=&quot;text\/html;charset=utf-8&quot; http-equiv=&quot;Content-Type&quot;&gt;\n    &lt;meta content=&quot;utf-8&quot; http-equiv=&quot;encoding&quot;&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;button id=&quot;mybutton&quot;&gt;\n        Click me!\n    &lt;\/button&gt;\n    &lt;script&gt;\n        var element = document.querySelector(&quot;#mybutton&quot;);\n        element.addEventListener('click', (event) =&gt; {\n            console.log(this); \/\/ window object\n            console.log(this.id); \/\/ undefined\n        }, false);\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p><strong>\u4e0e\u51fd\u6570\u8868\u8fbe\u5f0f\u8868\u73b0\u4e0d\u540c<\/strong>\uff0c\u7bad\u5934\u51fd\u6570\u4e2d\u7684 this \u662f\u6839\u636e\u5b9a\u4e49\u5b83\u7684\u4f4d\u7f6e\u51b3\u5b9a\u7684\uff0c\u800c\u4e0d\u662f\u5b83\u4f7f\u7528\u7684\u5730\u65b9\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>event.currentTarget<\/code> \u6765\u83b7\u53d6\u5143\u7d20\u3002<\/p>\n<p>\u603b\u7ed3\uff1a\u9664\u4e86\u81ea\u8eab\u76f4\u63a5\u4f7f\u7528\uff0c\u7bad\u5934\u51fd\u6570\u8868\u73b0\u7684\u4e0e\u51fd\u6570\u8868\u8fbe\u5f0f\u6709\u4e9b\u4e0d\u540c\u3002\u867d\u7136\u7bad\u5934\u51fd\u6570\u63d0\u4f9b\u4e86\u7b80\u660e\u7684\u8bed\u6cd5\u548c\u4e00\u4e9b\u4f18\u52bf\uff0c\u4f46\u8981\u77e5\u9053\u4f55\u65f6\u4e0d\u80fd\u4f7f\u7528\u5b83\u76f4\u63a5\u66ff\u6362\u51fd\u6570\u8868\u8fbe\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740\uff1ahttps:\/\/dev.to\/bhagatparwinder\/arrow-function&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,20,3],"tags":[191,104],"class_list":["post-1398","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-arrow","tag-function"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1398"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1398\/revisions"}],"predecessor-version":[{"id":1399,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1398\/revisions\/1399"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}