{"id":1019,"date":"2020-10-02T21:14:17","date_gmt":"2020-10-02T13:14:17","guid":{"rendered":"https:\/\/www.7gugu.com\/?p=1019"},"modified":"2020-10-02T21:14:17","modified_gmt":"2020-10-02T13:14:17","slug":"es6%e4%b8%ad%e7%9a%84%e7%ae%ad%e5%a4%b4%e5%87%bd%e6%95%b0%e4%b8%8efunction%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/7gugu.com\/index.php\/2020\/10\/02\/es6%e4%b8%ad%e7%9a%84%e7%ae%ad%e5%a4%b4%e5%87%bd%e6%95%b0%e4%b8%8efunction%e7%9a%84%e5%8c%ba%e5%88%ab\/","title":{"rendered":"ES6\u4e2d\u7684\u7bad\u5934\u51fd\u6570()=>\u4e0efunction\u7684\u533a\u522b"},"content":{"rendered":"\n<ol class=\"wp-block-list\"><li>\u5199\u6cd5\u4e0d\u540c<\/li><li>this\u6307\u5411\u4e0d\u540c<\/li><li>\u6784\u9020\u51fd\u6570<\/li><li>\u53d8\u91cf\u63d0\u5347<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u5199\u6cd5\u4e0d\u540c<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/function\nfunction x(a, b){\n  return a + b;\n}\n\/\/\u7bad\u5934\u51fd\u6570\nconst x = (a, b)=&gt;{\n  return a + b;\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2.this\u6307\u5411\u4e0d\u540c<\/h2>\n\n\n\n<p> \u4f7f\u7528<strong>function<\/strong>\u5b9a\u4e49\u7684\u51fd\u6570\uff0cthis\u7684\u6307\u5411\u968f\u7740\u8c03\u7528\u73af\u5883\u7684\u53d8\u5316\u800c<strong>\u53d8\u5316\u7684<\/strong>\uff0c\u800c<strong>\u7bad\u5934\u51fd\u6570<\/strong>\u4e2d\u7684this\u6307\u5411\u662f<strong>\u56fa\u5b9a\u4e0d\u53d8\u7684<\/strong>\uff0c\u4e00\u76f4\u6307\u5411\u7684\u662f\u5b9a\u4e49\u51fd\u6570\u7684\u73af\u5883\u3002 <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function x(a, b){\n  console.log(this);\n}\nconst obj = ()=&gt;{\n  test: 7gugu,\n}\nx(); \/\/Window\nobj.test(); \/\/obj { test: 7gugu }<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4f7f\u7528function\u5b9a\u4e49\u7684\u51fd\u6570\u4e2dthis\u6307\u5411\u662f\u968f\u7740\u8c03\u7528\u73af\u5883\u7684\u53d8\u5316\u800c\u53d8\u5316\u7684 <\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u5b9a\u4e49\u51fd\u6570 \nvar foo = () =&gt; { console.log(this) };\nvar obj = { aa:foo };\nfoo(); \/\/Window\nobj.aa(); \/\/Window<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u660e\u663e\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u7684\u65f6\u5019\uff0cthis\u7684\u6307\u5411\u662f\u6ca1\u6709\u53d1\u751f\u53d8\u5316\u7684\u3002 <\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u6784\u9020\u51fd\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\u4f7f\u7528function\u65b9\u6cd5\u5b9a\u4e49\u6784\u9020\u51fd\u6570 \nfunction Person(name, age){     \n  this.name = name;   \n  this.age = age; \n} \nvar lenhart =  new Person(lenhart, 25);\nconsole.log(lenhart); \/\/{name: 'lenhart', age: 25}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/\u5c1d\u8bd5\u4f7f\u7528\u7bad\u5934\u51fd\u6570 \nvar Person = (name, age) =&gt;{     \n  this.name = name;   \n  this.age = age; \n}; \nvar lenhart = new Person('lenhart', 25); \/\/Uncaught TypeError: Person is not a constructor<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>function\u662f\u53ef\u4ee5\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u7684\uff0c\u800c\u7bad\u5934\u51fd\u6570\u662f\u4e0d\u884c\u7684\u3002 <\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u53d8\u91cf\u63d0\u5347<\/h2>\n\n\n\n<p>\u7531\u4e8ejs\u7684\u5185\u5b58\u673a\u5236\uff0cfunction\u7684\u7ea7\u522b\u6700\u9ad8\uff0c\u800c\u7528\u7bad\u5934\u51fd\u6570\u5b9a\u4e49\u51fd\u6570\u7684\u65f6\u5019\uff0c\u9700\u8981var(let const\u5b9a\u4e49\u7684\u65f6\u5019\u66f4\u4e0d\u5fc5\u8bf4)\u5173\u952e\u8bcd\uff0c\u800cvar\u6240\u5b9a\u4e49\u7684\u53d8\u91cf\u4e0d\u80fd\u5f97\u5230\u53d8\u91cf\u63d0\u5347\uff0c\u6545\u7bad\u5934\u51fd\u6570\u4e00\u5b9a\u8981\u5b9a\u4e49\u4e8e\u8c03\u7528\u4e4b\u524d\uff01 <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">foo(); \/\/123\nfunction foo(){     \n  console.log('123');\n}  \narrowFn(); \/\/Uncaught TypeError: arrowFn is not a function \nvar arrowFn = () =&gt; {     \n  console.log('456'); \n};<\/pre>\n\n\n\n<p>\u8f6c\u8f7d\u81ea:<a href=\"https:\/\/blog.csdn.net\/github_38851471\/article\/details\/79446722\">https:\/\/blog.csdn.net\/github_38851471\/article\/details\/79446722<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u6cd5\u4e0d\u540c this\u6307\u5411\u4e0d\u540c \u6784\u9020\u51fd\u6570 \u53d8\u91cf\u63d0\u5347 1.\u5199\u6cd5\u4e0d\u540c \/\/function function x(a, &hellip; <a href=\"https:\/\/7gugu.com\/index.php\/2020\/10\/02\/es6%e4%b8%ad%e7%9a%84%e7%ae%ad%e5%a4%b4%e5%87%bd%e6%95%b0%e4%b8%8efunction%e7%9a%84%e5%8c%ba%e5%88%ab\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cES6\u4e2d\u7684\u7bad\u5934\u51fd\u6570()=>\u4e0efunction\u7684\u533a\u522b\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,13],"tags":[],"class_list":["post-1019","post","type-post","status-publish","format-standard","hentry","category-29","category-13"],"_links":{"self":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/1019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1019"}],"version-history":[{"count":0,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/1019\/revisions"}],"wp:attachment":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}