{"id":1248,"date":"2022-06-04T15:35:42","date_gmt":"2022-06-04T07:35:42","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1248"},"modified":"2023-02-16T14:43:45","modified_gmt":"2023-02-16T06:43:45","slug":"26-jian-tou-han-shu-ji-chu","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1248","title":{"rendered":"26 &#8211; 27 &#8211; \u7bad\u5934\u51fd\u6570\uff1a\u57fa\u7840 &#038; this \u5173\u952e\u5b57"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16543281843089.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/arrow-function-basics-34cm\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/arrow-function-basics-34cm<\/a><\/p>\n<h2><a id=\"%E7%AE%80%E4%BB%8B\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7b80\u4ecb<\/h2>\n<p>\u7bad\u5934\u51fd\u6570\u662f\u5728 ES6 \u5f15\u5165\u7684\uff0c\u76f8\u5bf9\u4e8e\u51fd\u6570\u8868\u8fbe\u5f0f\u6765\u8bf4\u662f\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u3002<\/p>\n<p>\u7bad\u5934\u51fd\u6570\u540d\u79f0\u7684\u6765\u6e90\u662f\u56e0\u4e3a\u4f7f\u7528\u4e86 <code>=&gt;<\/code>\u3002<\/p>\n<h3><a id=\"%E8%AF%AD%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u8bed\u6cd5<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const functionName = (arg1, arg2, ... argN) =&gt; {\n    return value;\n}\n<\/code><\/pre>\n<h3><a id=\"%E4%BE%8B%E5%AD%90\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f8b\u5b50<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const multiply = (a, b) =&gt; {\n    return a * b;\n}\n\nconsole.log(multiply(7, 8)); \/\/ 56\nconsole.log(multiply(3, 2)); \/\/ 6\n<\/code><\/pre>\n<h3><a id=\"%E5%85%B3%E9%94%AE%E7%89%B9%E7%82%B9\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5173\u952e\u7279\u70b9<\/h3>\n<ul>\n<li>\u7bad\u5934\u51fd\u6570\u7c7b\u4f3c\u533f\u540d\u51fd\u6570<\/li>\n<li>\u82e5\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\uff0c\u53ef\u4ee5\u7701\u7565\u5c0f\u62ec\u53f7<\/li>\n<\/ul>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">   const square = x =&gt; {\n       return x * x;\n   }\n\n   console.log(square(2)); \/\/ 4\n   console.log(square(7)); \/\/ 49\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u60c5\u5f62\u7684\u552f\u4e00\u9677\u9631\u662f\u5f53\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u4e14\u9700\u8981\u89e3\u6784\u65f6\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">    const foo = ({name = &quot;New User&quot;}) =&gt; name;\n    \n    console.log(foo({})); \/\/ New User\n    console.log(foo({name: &quot;Parwinder&quot;})); \/\/ Parwinder\n<\/code><\/pre>\n<ul>\n<li>\u82e5\u6ca1\u6709\u53c2\u6570\uff0c\u9700\u8981\u6dfb\u52a0\u4e0a\u5c0f\u62ec\u53f7<\/li>\n<\/ul>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">   const greeting = () =&gt; {\n       return &quot;Hello World!&quot;;\n   }\n\n   console.log(greeting()); \/\/ Hello World!\n<\/code><\/pre>\n<ul>\n<li>\u82e5\u51fd\u6570\u4f53\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e14\u53ea\u8fd4\u56de\u8be5\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u79fb\u51fa\u5c0f\u62ec\u53f7\u548c return \u5173\u952e\u5b57\u3002<\/li>\n<\/ul>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">   const greeting = () =&gt; &quot;Hello World!&quot;;\n   console.log(greeting()); \/\/ Hello World\n<\/code><\/pre>\n<p>\u73b0\u5728\u6211\u4eec\u77e5\u9053\u4e86\u6240\u6709\u7684\u5173\u952e\u7279\u70b9\uff0c\u8ba9\u6211\u4eec\u6765\u91cd\u5199\u83b7\u53d6\u6b63\u65b9\u5f62\u7684\u9762\u79ef\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const square = x =&gt; x * x;\nconsole.log(square(4)); \/\/ 16\n<\/code><\/pre>\n<h2><a id=\"this%E5%85%B3%E9%94%AE%E5%AD%97\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>this \u5173\u952e\u5b57<\/h2>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16543364837909.jpg\" alt=\"\" \/><\/p>\n<h3><a id=\"this%E5%85%B3%E9%94%AE%E5%AD%97%E5%92%8C%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>this \u5173\u952e\u5b57\u548c\u51fd\u6570<\/h3>\n<p>JavaScript \u4e2d\u7684 this \u5173\u952e\u5b57\u662f\u6267\u884c\u4e0a\u4e0b\u6587\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u5b83\u53ef\u80fd\u662f\u5168\u5c40\u7684\u3001\u51fd\u6570\u5185\u7684\u6216\u8005\u662f eval \u4e2d\u7684\u3002\u5bf9\u4e8e\u666e\u901a\u7684\u51fd\u6570\uff0cthis \u4f1a\u6839\u636e\u8c03\u7528\u5b83\u65b9\u5f0f\u4e0d\u540c\u800c\u53d8\u5316\u3002<\/p>\n<ol>\n<li>\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u65f6\uff0cthis \u6307\u5411\u5168\u5c40\u5bf9\u8c61\uff1b<\/li>\n<li>\u7528\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0cthis \u4ee3\u8868\u4e00\u4e2a\u65b0\u5bf9\u8c61\uff1b<\/li>\n<li>\u5f53\u51fd\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\uff0cthis \u5c31\u4ee3\u8868\u90a3\u4e2a\u5bf9\u8c61\uff1b<\/li>\n<li>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c this \u7684\u503c\u4e3a undefined\uff1b<\/li>\n<li>\u5728\u4e8b\u4ef6\u4e2d\uff0cthis \u6307\u5411\u63a5\u6536\u5230\u4e8b\u4ef6\u7684\u5143\u7d20\uff1b<\/li>\n<\/ol>\n<p>\u6211\u4eec\u4f7f\u7528\u8fd9\u79cd\u884c\u4e3a\u5df2\u7ecf\u5f88\u4e45\u4e86\uff0c\u4ee5\u81f3\u4e8e\u5927\u591a\u6570JavaScript\u5f00\u53d1\u8005\u90fd\u5df2\u7ecf\u4e60\u60ef\u4e86\u3002<\/p>\n<h3><a id=\"%E5%87%BD%E6%95%B0%E7%9B%B4%E6%8E%A5%E8%B0%83%E7%94%A8%EF%BC%8Cthis%E4%BB%A3%E8%A1%A8%E5%85%A8%E5%B1%80%E5%AF%B9%E8%B1%A1\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u51fd\u6570\u76f4\u63a5\u8c03\u7528\uff0cthis \u4ee3\u8868\u5168\u5c40\u5bf9\u8c61<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function foo() {\n    return this;\n};\n\nconsole.log(foo()); \/\/ window object in a browser, global object for node execution\n<\/code><\/pre>\n<h3><a id=\"%E7%94%A8%E5%9C%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E4%B8%AD%EF%BC%8Cthis%E4%BB%A3%E8%A1%A8%E4%B8%80%E4%B8%AA%E6%96%B0%E5%AF%B9%E8%B1%A1\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u7528\u5728\u6784\u9020\u51fd\u6570\u4e2d\uff0cthis \u4ee3\u8868\u4e00\u4e2a\u65b0\u5bf9\u8c61<\/h3>\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 = 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;);\n\nconsole.log(newOrder.order());\n\/\/ I will have sushi with soup and finish off with a yogurt\n<\/code><\/pre>\n<h3><a id=\"%E5%BD%93%E5%87%BD%E6%95%B0%E4%BD%9C%E4%B8%BA%E4%B8%80%E4%B8%AA%E5%AF%B9%E8%B1%A1%E7%9A%84%E6%96%B9%E6%B3%95%E8%A2%AB%E8%B0%83%E7%94%A8%EF%BC%8Cthis%E5%B0%B1%E4%BB%A3%E8%A1%A8%E9%82%A3%E4%B8%AA%E5%AF%B9%E8%B1%A1\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5f53\u51fd\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\uff0cthis \u5c31\u4ee3\u8868\u90a3\u4e2a\u5bf9\u8c61<\/h3>\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: function () {\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 butter chicken with rice and finish off with ice cream\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cthis \u6307\u5411 myObject,\u53ef\u4ee5\u83b7\u53d6\u5b83\u4e0a\u9762\u7684\u5c5e\u6027\u3002<\/p>\n<h3><a id=\"%E5%9C%A8%E4%B8%A5%E6%A0%BC%E6%A8%A1%E5%BC%8F%E4%B8%8B%EF%BC%8Cthis%E7%9A%84%E5%80%BC%E4%B8%BA-undefined\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0c this \u7684\u503c\u4e3a undefined<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">&quot;use strict&quot;;\nfunction foo() {\n    return this;\n};\n\nconsole.log(foo() === undefined); \/\/ true\n<\/code><\/pre>\n<h3><a id=\"%E5%9C%A8%E4%BA%8B%E4%BB%B6%E4%B8%AD%EF%BC%8Cthis%E6%8C%87%E5%90%91%E6%8E%A5%E6%94%B6%E5%88%B0%E4%BA%8B%E4%BB%B6%E7%9A%84%E5%85%83%E7%B4%A0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5728\u4e8b\u4ef6\u4e2d\uff0cthis \u6307\u5411\u63a5\u6536\u5230\u4e8b\u4ef6\u7684\u5143\u7d20<\/h3>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">&lt;!DOCTYPE html&gt;\n&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', function (event) {\n            console.log(this); \/\/ Refers to the button clicked\n            console.log(this.id); \/\/ mybutton\n        }, false);\n    &lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/\u524d\u7aef\u9ed1\u677f\u62a5-2.jpg\" alt=\"\u524d\u7aef\u9ed1\u677f\u62a5\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\uff1ahttps:\/\/dev.to\/bhagatparwinder\/arrow-function-b&#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":[249,2,20,3],"tags":[137,135],"class_list":["post-1248","post","type-post","status-publish","format-standard","hentry","category-javascript","category-all","category-frontend","category-tech","tag-arrow-function","tag-js-parwinder"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1248","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=1248"}],"version-history":[{"count":3,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1248\/revisions"}],"predecessor-version":[{"id":1252,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1248\/revisions\/1252"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}