{"id":671,"date":"2017-03-03T15:30:54","date_gmt":"2017-03-03T07:30:54","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=671"},"modified":"2017-03-03T15:06:27","modified_gmt":"2017-03-03T07:06:27","slug":"es6%e7%9a%84%e4%b8%80%e4%ba%9b%e6%96%b0%e7%89%b9%e6%80%a7","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=671","title":{"rendered":"ES6\u7684\u4e00\u4e9b\u65b0\u7279\u6027"},"content":{"rendered":"<h2>1.\u7bad\u5934\u51fd\u6570\uff08=&gt;\uff09<\/h2>\n<p>ES6\u4e2d\u5f15\u5165\u4e86\u7bad\u5934\u51fd\u6570\u7b26\u6765\u4ee3\u66fffunction\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u5b9a\u4e49\u65b9\u6cd5\u7684\u65f6\u5019\u53ef\u4ee5\u4e0d\u518d\u5199function,=&gt;\u7684\u5de6\u8fb9\u5373\u4e3a\u51fd\u6570\u540d\u548c\u53c2\u6570\uff0c\u53f3\u8fb9\u5373\u4e3a\u6267\u884c\u64cd\u4f5c\u548c\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">function(i){return i + 1; } \/\/ES5  \r\n(i) =&gt; i + 1 \/\/ES6<\/pre>\n<p>\u5982\u679c\u65b9\u6cd5\u6bd4\u8f83\u590d\u6742\uff0c\u5219\u6267\u884c\u7684\u64cd\u4f5c\u9700\u8981\u7528\uff5b\uff5d\u5305\u88f9\u8d77\u6765\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">function(x, y) {    \/\/ES5  \r\n    x++;  \r\n    y--;  \r\n    return x + y;  \r\n}     \r\n(x, y) =&gt; {x++; y--; return x+y} \/\/ES6<\/pre>\n<p>\u5f15\u5165\u7bad\u5934\u51fd\u6570\u4e0d\u4ec5\u4f7f\u5f97\u5199\u6cd5\u66f4\u52a0\u7b80\u6d01\u6e05\u6670\uff0c\u540c\u65f6\u4e5f\u89e3\u51b3\u4e86js\u4e2dthis\u4f5c\u7528\u57df\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5728ES5\u4e2d\uff0c\u51fd\u6570\u5f53\u4e2d\u7684this\u4ec5\u6307\u4ee3\u5f53\u524d\u51fd\u6570\uff0c\u5982\u679c\u51fd\u6570\u5f53\u4e2d\u8fd8\u5305\u542b\u51fd\u6570\uff0c\u90a3\u4e48this\u7684\u4f7f\u7528\u5c31\u9700\u8981\u76f8\u5f53\u5c0f\u5fc3\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">class Animal {  \r\n    constructor(){  \r\n        this.type = 'animal'    \/\/\u8fd9\u91cc\u7684this\u6307\u4ee3Animal\u5bf9\u8c61  \r\n    }  \r\n    says(say){  \r\n        setTimeout(function(){  \r\n            console.log(this.type + ' says ' + say)\/\/\u8fd9\u91cc\u7684this\u7d27\u6307\u4ee3setTimeout\u7684\u6267\u884c\u51fd\u6570\uff0c\u800c\u8be5\u51fd\u6570\u4e2d\u5e76\u6ca1\u6709type\u5c5e\u6027  \r\n        }, 1000)  \r\n    }  \r\n}  \r\n  \r\n var animal = new Animal()  \r\n animal.says('hi')  \/\/undefined says hi  \r\n  \r\n\u4f20\u7edf\u7684\u505a\u6cd5\u6709\u4ee5\u4e0b2\u79cd\uff1a  \r\n\u7b2c\u4e00\u79cd\uff1a\u58f0\u660e\u4e00\u4e2a\u53d8\u91cf\u6307\u4ee3\u6700\u5916\u5c42\u5bf9\u8c61\u7684this\uff0c\u7136\u540e\u5728\u5185\u5c42\u51fd\u6570\u4e2d\u4f7f\u7528\u8be5\u53d8\u91cf\u3002\u4f8b\u5982\uff1a  \r\nsays(say){  \r\n       var self = this;  \r\n       setTimeout(function(){  \r\n           console.log(self.type + ' says ' + say)  \r\n       }, 1000)  \r\n}<\/pre>\n<p>\u7b2c\u4e8c\u79cd\uff1a\u4f7f\u7528bind(this),bind\u65b9\u6cd5\u4f1a\u65b0\u521b\u5efa\u4e00\u4e2a\u7ed1\u5b9a\u51fd\u6570\uff0c\u5f53\u8c03\u7528\u8fd9\u4e2a\u7ed1\u5b9a\u51fd\u6570\u7684\u65f6\u5019\u4f1a\u4ee5bind\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4fee\u6539\u51fd\u6570\u5185\u90e8\u7684this\u6307\u5411\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">says(say){  \r\n   setTimeout(function(){  \r\n       console.log(this.type + ' says ' + say)  \r\n   }.bind(this), 1000)\/\/\u7528says\u7684this\u5bf9\u8c61\u66ff\u4ee3setTimeout\u6267\u884c\u65b9\u6cd5\u7684this\u5bf9\u8c61  \r\n}<\/pre>\n<p>\u7531\u4e8e\u7bad\u5934\u51fd\u6570\u5185\u90e8\u5e76\u6ca1\u6709\u5b9a\u4e49this\u5bf9\u8c61\uff0c\u6240\u4ee5\u51fd\u6570\u5185\u90e8\u7684this\u5b8c\u5168\u662f\u7ee7\u627f\u7684\u5916\u90e8\u7684\uff0c\u6240\u4ee5ES6\u4e2d\u5c06\u4e0d\u5fc5\u62c5\u5fc3\u4e0a\u9762\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">lass Animal {  \r\n    constructor(){  \r\n        this.type = 'animal'  \r\n    }  \r\n    says(say){  \r\n        setTimeout( () =&gt; {  \r\n            console.log(this.type + ' says ' + say)  \r\n        }, 1000)  \r\n    }  \r\n}  \r\n var animal = new Animal()  \r\n animal.says('hi')  \/\/animal says hi<\/pre>\n<h2>2.\u53d8\u91cf\u58f0\u660e\uff08let\u3001const\uff09<\/h2>\n<p>ES6\u4e2d\u65b0\u5f15\u5165\u4e862\u4e2a\u53d8\u91cf\u7684\u58f0\u660elet\u548cconst\uff0c\u4ed6\u4eec\u7684\u4f5c\u7528\u57fa\u672c\u8ddfvar\u76f8\u540c\uff0c\u5177\u4f53\u533a\u522b\u5982\u4e0b\uff1a<br \/>\nvar \u58f0\u660e\u7684\u53d8\u91cf\u4f5c\u7528\u5728\u5168\u5c40\u6216\u8005\u65b9\u6cd5\u4f53\u5185\u3002\u5757\u7ea7\u53d8\u91cf\u53ea\u80fd\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6216\u65b9\u6cd5\u4f53\u7684\u53d8\u91cf\u3002<\/p>\n<p>let\u58f0\u660e\u7684\u53d8\u91cf\u53ea\u4f5c\u7528\u5728\u4ee3\u7801\u5757\u3002\u53ef\u4ee5\u5b8c\u5168\u4f7f\u7528let\u4ee3\u66ffvar\u3002<\/p>\n<p>const\u53ea\u662f\u7528\u6765\u58f0\u660e\u5e38\u91cf\u7684\uff0c\u4e14\u4e00\u65e6\u58f0\u660e\u5e38\u91cf\u7684\u503c\u5c31\u4e0d\u80fd\u6539\u53d8\u4e86\u3002\u80fd\u591f\u66f4\u6e05\u6670\u7684\u8868\u660e\u8fd9\u662f\u5e38\u91cf\uff0c\u4e0d\u80fd\u4fee\u6539\u3002<\/p>\n<p>\u5148\u6765\u770b\u4e00\u4e2alet\u548cvar\u4f7f\u7528\u533a\u522b\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">var name = 'zach'  \r\n  \r\nwhile (true) {  \r\n    var name = 'obama'  \r\n    console.log(name)  \/\/obama  \r\n    break  \r\n}  \r\n  \r\nconsole.log(name)  \/\/obama  \r\n\/\/\u7531\u4e8evar\u4f5c\u7528\u57df\u53ea\u6709\u5168\u5c40\u548c\u51fd\u6570\u5185\u90e8\uff0c\u6240\u4ee5\u7b2c\u4e8c\u6b21\u58f0\u660e\u7684\u4f5c\u7528\u57df\u8ddf\u7b2c\u4e00\u6b21\u58f0\u660e\u4e00\u81f4\u3002<\/pre>\n<pre class=\"brush:js;toolbar:false\">let name = 'zach'  \r\n  \r\nwhile (true) {  \r\n    let name = 'obama'  \r\n    console.log(name)  \/\/obama  \r\n    break  \r\n}  \r\n  \r\nconsole.log(name)  \/\/zach  \r\n\/\/let\u7684\u4f5c\u7528\u57df\u53ea\u5b58\u5728\u4e8e\u5757\u7ea7\u5185\u90e8\u3002<\/pre>\n<p>var\u7531\u4e8e\u4f5c\u7528\u57df\u7684\u95ee\u9898\u5b58\u5728\u5faa\u73af\u53d8\u91cf\u6cc4\u6f0f\u4e3a\u5168\u5c40\u53d8\u91cf\u7684\u95ee\u9898\u3002\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">var a = [];  \r\nfor (var i = 0; i &lt; 10; i++) {  \r\n  a[i] = function () {  \r\n    console.log(i);  \r\n  };  \r\n}  \r\na[6](); \/\/ 10<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u5c31\u662f\u56e0\u4e3ai\u88ab\u6cc4\u6f0f\u4e3a\u4e86\u5168\u5c40\u53d8\u91cf\uff0c\u5f53\u5faa\u73af\u5b8c\u6210\u540ei\u5df2\u7ecf\u4e3a10\u4e86\uff0c\u6240\u4ee5\u65e0\u8bba\u6570\u7ec4\u4e0b\u6807\u662f\u51e0\u7684\u7ed3\u679c\u90fd\u662f10\u3002<\/p>\n<p>\u4f20\u7edf\u7684\u89e3\u51b3\u65b9\u6848\u662f\u91c7\u7528\u95ed\u5305\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">var a = [];  \r\nfor (var i = 0; i &lt; 10; i++) {  \r\n  a[i] = getA(i);  \r\n}  \r\nfunction getA(i){  \r\n  var showA = function () {  \r\n    console.log(i);  \r\n  };  \r\n  \r\n  return showA;  \r\n}  \r\n  \r\na[6](); \/\/ 6<\/pre>\n<p>ES6\u4f7f\u7528let\u7684\u89e3\u51b3\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">var a = [];  \r\nfor (let i = 0; i &lt; 10; i++) {  \r\n  a[i] = function () {  \r\n    console.log(i);  \r\n  };  \r\n}  \r\na[6](); \/\/ 6<\/pre>\n<p>const\u53ea\u662f\u7528\u6765\u58f0\u660e\u5e38\u91cf\uff0c\u5f53\u6211\u4eec\u8bd5\u56fe\u4fee\u6539\u5b83\u58f0\u660e\u7684\u5e38\u91cf\u65f6\uff0c\u6d4f\u89c8\u5668\u4f1a\u62a5\u9519\u3002<\/p>\n<p>\u5176\u6700\u597d\u7684\u5e94\u7528\u573a\u666f\u662f\uff0c\u5f53\u6211\u4eec\u5f15\u5165\u7b2c\u4e09\u65b9\u63d2\u4ef6\u5e93\u7684\u65f6\u5019\u53ef\u4ee5\u4fdd\u8bc1\u63d2\u4ef6\u58f0\u660e\u4e0d\u91cd\u590d\u3002<\/p>\n<p>\u9700\u6ce8\u610f\uff1a\u9759\u6001\u5b57\u7b26\u4e32\u58f0\u660e\u7edf\u4e00\u4f7f\u7528\u5355\u5f15\u53f7\u6216\u53cd\u5f15\u53f7\uff0c\u4e0d\u4f7f\u7528\u53cc\u5f15\u53f7\uff0c\u52a8\u6001\u5b57\u7b26\u4e32\u4f7f\u7528\u53cd\u5f15\u53f7\u3002\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush:js;toolbar:false\">const PI = Math.PI  \r\nconst monent = require('moment')  \r\nconst b = `foo${a}bar`;<\/pre>\n<h2>3.\u7c7b\u7684\u652f\u6301<\/h2>\n<h2>4.\u5b57\u7b26\u4e32\u6a21\u7248<\/h2>\n<h2>5.\u53c2\u6570<\/h2>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=671\">ES6\u7684\u4e00\u4e9b\u65b0\u7279\u6027<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>1.\u7bad\u5934\u51fd\u6570\uff08=&gt;\uff09 ES6\u4e2d\u5f15\u5165\u4e86\u7bad\u5934\u51fd\u6570\u7b26\u6765\u4ee3\u66fffunction\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u5b9a\u4e49\u65b9\u6cd5\u7684\u65f6\u5019\u53ef\u4ee5\u4e0d\u518d\u5199function,=&gt;\u7684\u5de6\u8fb9\u5373\u4e3a\u51fd\u6570\u540d\u548c\u53c2\u6570\uff0c\u53f3\u8fb9\u5373\u4e3a\u6267\u884c\u64cd\u4f5c\u548c\u8fd4\u56de\u503c\u3002 \u4f8b\u5982\uff1a f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":317,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[143],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/671"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=671"}],"version-history":[{"count":3,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/671\/revisions"}],"predecessor-version":[{"id":674,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/671\/revisions\/674"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/media\/317"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=671"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}