{"id":1264,"date":"2022-06-12T00:17:36","date_gmt":"2022-06-11T16:17:36","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=1264"},"modified":"2022-06-12T01:02:27","modified_gmt":"2022-06-11T17:02:27","slug":"33-jian-tou-han-shu-he-new-arguments-yi-ji-super","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1264","title":{"rendered":"33 &#8211; \u7bad\u5934\u51fd\u6570\u548c new \u3001arguments \u4ee5\u53ca super \u5173\u952e\u5b57"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/16549643679712.jpg\" alt=\"\" \/><\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"https:\/\/dev.to\/bhagatparwinder\/arrow-function-and-the-new-arguments-super-keyword-2d1l\" target=\"_blank\" rel=\"noopener\">https:\/\/dev.to\/bhagatparwinder\/arrow-function-and-the-new-arguments-super-keyword-2d1l<\/a><\/p>\n<p>\u6211\u4eec\u524d\u9762\u5df2\u7ecf\u5b66\u4e86\u6709\u5173\u7bad\u5934\u51fd\u6570\u4ee5\u53ca\u5b83\u4e0e\u5176\u4ed6\u51fd\u6570\u7684\u4e0d\u540c\uff0c\u8fd8\u6709\u5173\u952e\u5b57 this \u7684\u5185\u5bb9\u3002<\/p>\n<p>\u5f53\u6d89\u53ca\u5230 this \u5173\u952e\u5b57\u7684\u65f6\u5019\u7bad\u5934\u51fd\u6570\u8868\u73b0\u7684\u6709\u70b9\u4e0d\u540c\uff0c\u5b83\u5185\u90e8\u6ca1\u6709\u7ed1\u5b9a <code>arguments<\/code>\u3001<code>new<\/code> \u548c <code>super<\/code> \u5173\u952e\u5b57\uff01<\/p>\n<h2><a id=\"arguments\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Arguments<\/h2>\n<p><code>arguments<\/code> \u5bf9\u8c61\u662f\u4e00\u4e2a\u7c7b\u6570\u7ec4\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u4f20\u5165\u51fd\u6570\u7684\u53c2\u6570\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">function addThreeNumbers(a, b, c) {\n    console.log(arguments.length); \/\/ 3\n    console.log(arguments[0]); \/\/ 4\n    console.log(arguments[1]); \/\/ 17\n    console.log(arguments[2]); \/\/ 22\n    return a + b + c;\n}\n\nconsole.log(addThreeNumbers(4, 17, 22)); \/\/ 43\n<\/code><\/pre>\n<p>\u7bad\u5934\u51fd\u6570\u7684 <code>arguments<\/code> \u662f\u5bf9\u5305\u56f4\u8303\u56f4\u5185\u7684\u53c2\u6570\u7684\u5f15\u7528\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const bar = x =&gt; console.log(arguments);\n\nconsole.log(bar()); \/\/ Uncaught ReferenceError: arguments is not defined\n<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u53d8\u901a\u7684\u65b9\u6cd5\u6765\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u5c31\u662f\u4f7f\u7528 <code>rest<\/code> \u64cd\u4f5c\u7b26\u6765\u83b7\u53d6\u4f20\u5165\u51fd\u6570\u7684\u53c2\u6570\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const addThreeNumbers = (...args) =&gt; {\n    console.log(args.length); \/\/ 3\n    console.log(args[0]); \/\/ 4\n    console.log(args[1]); \/\/ 17\n    console.log(args[2]); \/\/ 22\n    return args[0] + args[1] + args[2];\n}\n\nconsole.log(addThreeNumbers(4, 17, 22)); \/\/ 43\n<\/code><\/pre>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u89e3\u6784\u6765\u4f7f\u4f8b\u5b50\u66f4\u7b80\u6d01\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const addThreeNumbers = (...args) =&gt; {\n\n    const [a, b, c] = args;\n\n    console.log(args.length); \/\/ 3\n    console.log(a); \/\/ 4\n    console.log(b); \/\/ 17\n    console.log(c); \/\/ 22\n\n    return a + b + c;\n}\n\nconsole.log(addThreeNumbers(4, 17, 22)); \/\/ 43\n<\/code><\/pre>\n<h2><a id=\"new\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>New<\/h2>\n<p>\u7bad\u5934\u51fd\u6570\u4e0d\u80fd\u4f5c\u4e3a\u6784\u9020\u51fd\u6570\uff0c\u5f53 <code>new<\/code> \u4e0e\u7bad\u5934\u51fd\u6570\u4e00\u8d77\u4f7f\u7528\u7684\u65f6\u5019\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">const foo = () =&gt; { };\nconst bar = new foo(); \/\/ foo is not a constructor\n<\/code><\/pre>\n<p>\u662f\u56e0\u4e3a\u7bad\u5934\u51fd\u6570\u7f3a\u5931\u4e86\u4e00\u4e2a\u5185\u90e8\u65b9\u6cd5 <a href=\"https:\/\/262.ecma-international.org\/6.0\/#table-6\" target=\"_blank\" rel=\"noopener\">Construct<\/a><\/p>\n<h2><a id=\"super\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>Super<\/h2>\n<p>\u6839\u636e ES \u89c4\u8303\u6211\u4eec\u4f9d\u65e7\u4e0d\u80fd\u5728\u7bad\u5934\u51fd\u6570\u4e2d\u4f7f\u7528 <code>super<\/code> \u5173\u952e\u5b57\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">class Base {\n    foo = () =&gt; {\n        console.log(&quot;Hello&quot;);\n    }\n}\n\nclass Child extends Base {\n    bar() {\n        super.foo(); \/\/ Only public and protected methods of the base class are accessible via the 'super' keyword.\n    };\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u6211\u4eec\u53ef\u4ee5\u7528\u5e38\u89c4\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">class Base {\n    foo() {\n        console.log(&quot;Hello&quot;);\n    }\n}\n\nclass Child extends Base {\n    bar() {\n        super.foo();\n    };\n}\n<\/code><\/pre>\n<h2><a id=\"%E5%85%B6%E4%BB%96\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5176\u4ed6<\/h2>\n<ul>\n<li>\u7bad\u5934\u51fd\u6570\u6ca1\u6709 <code>prototype<\/code> \u5c5e\u6027<\/li>\n<\/ul>\n<pre class=\"line-numbers\"><code class=\"language-plain_text\">   var Foo = () =&gt; { };\n   console.log(Foo.prototype); \/\/ undefined\n<\/code><\/pre>\n<ul>\n<li>\u7bad\u5934\u51fd\u6570\u4e5f\u4e0d\u80fd\u7528\u4f5c generators ,\u4ed6\u4eec\u6ca1\u6709 <code>yield<\/code> \u5173\u952e\u5b57\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/06\/\u524d\u7aef\u9ed1\u677f\u62a5-5.jpg\" alt=\"\u524d\u7aef\u9ed1\u677f\u62a5\" \/><\/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":[142,137,6,143],"class_list":["post-1264","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-arguments","tag-arrow-function","tag-javascript","tag-super"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1264","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=1264"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1264\/revisions"}],"predecessor-version":[{"id":1265,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1264\/revisions\/1265"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}