{"id":784,"date":"2021-03-08T23:26:19","date_gmt":"2021-03-08T15:26:19","guid":{"rendered":"https:\/\/www.shineskr.com\/?p=784"},"modified":"2021-03-09T22:09:34","modified_gmt":"2021-03-09T14:09:34","slug":"es6%e7%9a%84%e6%96%b0%e7%89%b9%e6%80%a7","status":"publish","type":"post","link":"https:\/\/www.shineskr.com\/784.html","title":{"rendered":"ES6\u7684\u65b0\u7279\u6027"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression-->\n<h2 class=\"wp-block-heading\"> <strong>ES6 \u7b80\u4ecb<\/strong> <\/h2>\n\n\n\n<p>ECMAScript 6 \u7b80\u79f0 ES6\uff0c\u662f JavaScript \u8bed\u8a00\u7684\u4e0b\u4e00\u4ee3\u6807\u51c6\uff0c\u5df2\u7ecf\u57282015\u5e746\u6708\u6b63\u5f0f\u53d1\u5e03\u4e86\u3002\u5b83\u7684\u76ee\u6807\u662f\u4f7f\u5f97 JavaScript \u8bed\u8a00\u53ef\u4ee5\u7528\u6765\u7f16\u5199\u590d\u6742\u7684\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\uff0c\u6210\u4e3a\u4f01\u4e1a\u7ea7\u5f00\u53d1\u8bed\u8a00\u3002<\/p>\n\n\n\n<p>ECMAScript \u548c JavaScript \u7684\u5173\u7cfb\uff1a\u524d\u8005\u662f\u540e\u8005\u7684\u8bed\u6cd5\u89c4\u683c\uff0c\u540e\u8005\u662f\u524d\u8005\u7684\u4e00\u79cd\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>Babel\uff1a\u5c06ES6\u4ee3\u7801\u8f6c\u4e3aES5\u4ee3\u7801&nbsp;<a rel=\"noreferrer noopener\" href=\"http:\/\/babeljs.io\/\" target=\"_blank\">http:\/\/babeljs.io\/<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> <strong>\u65b0\u7279\u6027<\/strong>\uff082015\uff09<\/h2>\n\n\n\n<p>ES6\u7684\u7279\u6027\u6bd4\u8f83\u591a\uff0c\u5728 ES5 \u53d1\u5e03\u8fd1 6 \u5e74\uff082009-11 \u81f3 2015-6\uff09\u4e4b\u540e\u624d\u5c06\u5176\u6807\u51c6\u5316\u3002\u4e24\u4e2a\u53d1\u5e03\u7248\u672c\u4e4b\u95f4\u65f6\u95f4\u8de8\u5ea6\u5f88\u5927\uff0c\u6240\u4ee5ES6\u4e2d\u7684\u7279\u6027\u6bd4\u8f83\u591a\u3002 \u5728\u8fd9\u91cc\u5217\u4e3e\u51e0\u4e2a\u5e38\u7528\u7684\uff1a<\/p>\n\n\n\n<ul><li>\u7c7b<\/li><li>\u6a21\u5757\u5316<\/li><li>\u7bad\u5934\u51fd\u6570<\/li><li>\u51fd\u6570\u53c2\u6570\u9ed8\u8ba4\u503c<\/li><li>\u6a21\u677f\u5b57\u7b26\u4e32<\/li><li>\u89e3\u6784\u8d4b\u503c<\/li><li>\u5ef6\u5c55\u64cd\u4f5c\u7b26<\/li><li>\u5bf9\u8c61\u5c5e\u6027\u7b80\u5199<\/li><li>Promise<\/li><li>Let\u4e0eConst<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"> 1.\u7c7b\uff08class\u3001<strong>extends\u3001super<\/strong> \uff09 <\/h3>\n\n\n\n<p>ES5\u4e2d\u6700\u4ee4\u4eba\u5934\u75bc\u7684\u7684\u51e0\u4e2a\u90e8\u5206\uff1a\u539f\u578b\u3001\u6784\u9020\u51fd\u6570\uff0c\u7ee7\u627f\uff0c\u6709\u4e86ES6\u6211\u4eec\u4e0d\u518d\u70e6\u607c\uff01<\/p>\n\n\n\n<p>ES6\u5f15\u5165\u4e86Class\uff08\u7c7b\uff09\u8fd9\u4e2a\u6982\u5ff5\u3002<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nclass Animal {\nconstructor() {\n\u3000\u3000this.type = 'animal';\n}\nsays(say) {\n\u3000\u3000console.log(this.type + ' says ' + say);\n}\n}\n \nlet animal = new Animal();\nanimal.says('hello'); \/\/animal says hello\n \nclass Cat extends Animal {\n\u3000\u3000constructor() {\n\u3000\u3000\u3000\u3000super();\n\u3000\u3000\u3000\u3000this.type = 'cat';\n\u3000\u3000}\n}\n \nlet cat = new Cat();\ncat.says('hello'); \/\/cat says hello\n<\/pre>\n\n\n<p>\u4e0a\u9762\u4ee3\u7801\u9996\u5148\u7528class\u5b9a\u4e49\u4e86\u4e00\u4e2a\u201c\u7c7b\u201d\uff0c\u53ef\u4ee5\u770b\u5230\u91cc\u9762\u6709\u4e00\u4e2aconstructor\u65b9\u6cd5\uff0c\u8fd9\u5c31\u662f\u6784\u9020\u65b9\u6cd5\uff0c\u800cthis\u5173\u952e\u5b57\u5219\u4ee3\u8868\u5b9e\u4f8b\u5bf9\u8c61\u3002\u7b80\u5355\u5730\u8bf4\uff0cconstructor\u5185\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u662f\u5b9e\u4f8b\u5bf9\u8c61\u81ea\u5df1\u7684\uff0c\u800cconstructor\u5916\u5b9a\u4e49\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u5219\u662f\u6240\u6709\u5b9e\u529b\u5bf9\u8c61\u53ef\u4ee5\u5171\u4eab\u7684\u3002<\/p>\n\n\n\n<p>Class\u4e4b\u95f4\u53ef\u4ee5\u901a\u8fc7extends\u5173\u952e\u5b57\u5b9e\u73b0\u7ee7\u627f\uff0c\u8fd9\u6bd4ES5\u7684\u901a\u8fc7\u4fee\u6539\u539f\u578b\u94fe\u5b9e\u73b0\u7ee7\u627f\uff0c\u8981\u6e05\u6670\u548c\u65b9\u4fbf\u5f88\u591a\u3002\u4e0a\u9762\u5b9a\u4e49\u4e86\u4e00\u4e2aCat\u7c7b\uff0c\u8be5\u7c7b\u901a\u8fc7extends\u5173\u952e\u5b57\uff0c\u7ee7\u627f\u4e86Animal\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>super\u5173\u952e\u5b57\uff0c\u5b83\u6307\u4ee3\u7236\u7c7b\u7684\u5b9e\u4f8b\uff08\u5373\u7236\u7c7b\u7684this\u5bf9\u8c61\uff09\u3002\u5b50\u7c7b\u5fc5\u987b\u5728constructor\u65b9\u6cd5\u4e2d\u8c03\u7528super\u65b9\u6cd5\uff0c\u5426\u5219\u65b0\u5efa\u5b9e\u4f8b\u65f6\u4f1a\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a\u5b50\u7c7b\u6ca1\u6709\u81ea\u5df1\u7684this\u5bf9\u8c61\uff0c\u800c\u662f\u7ee7\u627f\u7236\u7c7b\u7684this\u5bf9\u8c61\uff0c\u7136\u540e\u5bf9\u5176\u8fdb\u884c\u52a0\u5de5\u3002\u5982\u679c\u4e0d\u8c03\u7528super\u65b9\u6cd5\uff0c\u5b50\u7c7b\u5c31\u5f97\u4e0d\u5230this\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>ES6\u7684\u7ee7\u627f\u673a\u5236\uff0c\u5b9e\u8d28\u662f\u5148\u521b\u9020\u7236\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61this\uff08\u6240\u4ee5\u5fc5\u987b\u5148\u8c03\u7528super\u65b9\u6cd5\uff09\uff0c\u7136\u540e\u518d\u7528\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4fee\u6539this\u3002<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ ES5\nvar Shape = function(id, x, y) {\n\u3000\u3000this.id = id,\n\u3000\u3000this.move(x, y);\n};\nShape.prototype.move = function(x, y) {\n\u3000\u3000this.x = x;\n\u3000\u3000this.y = y;\n};\n \nvar Rectangle = function id(ix, x, y, width, height) {\n\u3000\u3000Shape.call(this, id, x, y);\n\u3000\u3000this.width = width;\n\u3000\u3000this.height = height;\n};\nRectangle.prototype = Object.create(Shape.prototype);\nRectangle.prototype.constructor = Rectangle;\n \nvar Circle = function(id, x, y, radius) {\n\u3000\u3000Shape.call(this, id, x, y);\n\u3000\u3000this.radius = radius;\n};\nCircle.prototype = Object.create(Shape.prototype);\nCircle.prototype.constructor = Circle;\n \n\/\/ ES6\nclass Shape {\n\u3000\u3000constructor(id, x, y) {\n\u3000\u3000\u3000\u3000this.id = id this.move(x, y);\n\u3000\u3000}\n\u3000\u3000move(x, y) {\n\u3000\u3000\u3000\u3000this.x = x this.y = y;\n\u3000\u3000}\n}\n \nclass Rectangle extends Shape {\n\u3000\u3000constructor(id, x, y, width, height) {\n\u3000\u3000\u3000\u3000super(id, x, y) this.width = width this.height = height;\n\u3000\u3000}\n}\n \nclass Circle extends Shape {\n\u3000\u3000constructor(id, x, y, radius) {\n\u3000\u3000\u3000\u3000super(id, x, y) this.radius = radius;\n\u3000\u3000}\n}\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\"> 2.\u6a21\u5757\u5316(Module) <\/h2>\n\n\n\n<p>ES5\u4e0d\u652f\u6301\u539f\u751f\u7684\u6a21\u5757\u5316\uff0c\u5728ES6\u4e2d\u6a21\u5757\u4f5c\u4e3a\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\u88ab\u6dfb\u52a0\u8fdb\u6765\u3002\u6a21\u5757\u7684\u529f\u80fd\u4e3b\u8981\u7531 export \u548c import \u7ec4\u6210\u3002\u6bcf\u4e00\u4e2a\u6a21\u5757\u90fd\u6709\u81ea\u5df1\u5355\u72ec\u7684\u4f5c\u7528\u57df\uff0c\u6a21\u5757\u4e4b\u95f4\u7684\u76f8\u4e92\u8c03\u7528\u5173\u7cfb\u662f\u901a\u8fc7 export \u6765\u89c4\u5b9a\u6a21\u5757\u5bf9\u5916\u66b4\u9732\u7684\u63a5\u53e3\uff0c\u901a\u8fc7import\u6765\u5f15\u7528\u5176\u5b83\u6a21\u5757\u63d0\u4f9b\u7684\u63a5\u53e3\u3002\u540c\u65f6\u8fd8\u4e3a\u6a21\u5757\u521b\u9020\u4e86\u547d\u540d\u7a7a\u95f4\uff0c\u9632\u6b62\u51fd\u6570\u7684\u547d\u540d\u51b2\u7a81\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5bfc\u51fa(export)<\/h4>\n\n\n\n<p>ES6\u5141\u8bb8\u5728\u4e00\u4e2a\u6a21\u5757\u4e2d\u4f7f\u7528export\u6765\u5bfc\u51fa\u591a\u4e2a\u53d8\u91cf\u6216\u51fd\u6570\u3002<\/p>\n\n\n\n<p>export var name = 'Rainbow'<\/p>\n\n\n\n<p>ES6\u4e0d\u4ec5\u652f\u6301\u53d8\u91cf\u7684\u5bfc\u51fa\uff0c\u4e5f\u652f\u6301\u5e38\u91cf\u7684\u5bfc\u51fa\u3002<br>export const sqrt =Math.sqrt;\/\/\u5bfc\u51fa\u5e38\u91cf <\/p>\n\n\n\n<p> ES6\u5c06\u4e00\u4e2a\u6587\u4ef6\u89c6\u4e3a\u4e00\u4e2a\u6a21\u5757\uff0c\u4e0a\u9762\u7684\u6a21\u5757\u901a\u8fc7 export \u5411\u5916\u8f93\u51fa\u4e86\u4e00\u4e2a\u53d8\u91cf\u3002\u4e00\u4e2a\u6a21\u5757\u4e5f\u53ef\u4ee5\u540c\u65f6\u5f80\u5916\u9762\u8f93\u51fa\u591a\u4e2a\u53d8\u91cf\u3002 <\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvar name = 'Rainbow';\nvar age = '24';\n\nexport {\n  name,\n  age\n};\n<\/pre>\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/\u5bfc\u51fa\u51fd\u6570\nexport function myModule (someArg) {\n    return someArg;\n}\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"> \u5bfc\u5165(import) <\/h4>\n\n\n\n<p> \u5b9a\u4e49\u597d\u6a21\u5757\u7684\u8f93\u51fa\u4ee5\u540e\u5c31\u53ef\u4ee5\u5728\u53e6\u5916\u4e00\u4e2a\u6a21\u5757\u901a\u8fc7import\u5f15\u7528\u3002 <\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nimport myModule from 'myModule';\nimport { name ,age } from 'test';\n<\/pre>\n\n\n<p> 1.\u4e00\u6761import \u8bed\u53e5\u53ef\u4ee5\u540c\u65f6\u5bfc\u5165\u9ed8\u8ba4\u51fd\u6570\u548c\u5176\u5b83\u53d8\u91cf\u3002<br> \u5982\uff1aimport defaultMethod, { otherMethod } from 'xxx.js'; <\/p>\n\n\n\n<p>2.\u53ef\u4ee5\u4e3a\u53d8\u91cf\u8d77\u522b\u540d<br> \u5982\uff1aimport { otherMethod  as om } from 'xxx.js'<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> 3.\u7bad\u5934\uff08Arrow\uff09\u51fd\u6570 <\/h2>\n\n\n\n<p>\u8fd9\u662fES6\u4e2d\u6700\u4ee4\u4eba\u6fc0\u52a8\u7684\u7279\u6027\u4e4b\u4e00\u3002 =&gt;\u4e0d\u53ea\u662f\u5173\u952e\u5b57function\u7684\u7b80\u5199\uff0c\u5b83\u8fd8\u5e26\u6765\u4e86\u5176\u5b83\u597d\u5904\u3002\u7bad\u5934\u51fd\u6570\u4e0e\u5305\u56f4\u5b83\u7684\u4ee3\u7801\u5171\u4eab\u540c\u4e00\u4e2a this,\u80fd\u5e2e\u4f60\u5f88\u597d\u7684\u89e3\u51b3this\u7684\u6307\u5411\u95ee\u9898\u3002\u6709\u7ecf\u9a8c\u7684JavaScript\u5f00\u53d1\u8005\u90fd\u719f\u6089\u8bf8\u5982 var self = this;\u6216 var that =this\u8fd9\u79cd\u5f15\u7528\u5916\u56f4this\u7684\u6a21\u5f0f\u3002\u4f46\u501f\u52a9 =&gt;\uff0c\u5c31\u4e0d\u9700\u8981\u8fd9\u79cd\u6a21\u5f0f\u4e86\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u7bad\u5934\u51fd\u6570\u7684\u7ed3\u6784<\/h5>\n\n\n\n<p>\u7bad\u5934\u51fd\u6570\u7684\u7bad\u5934=&gt;\u4e4b\u524d\u662f\u4e00\u4e2a\u7a7a\u62ec\u53f7\u3001\u5355\u4e2a\u7684\u53c2\u6570\u540d\u3001\u6216\u7528\u62ec\u53f7\u62ec\u8d77\u7684\u591a\u4e2a\u53c2\u6570\u540d\uff0c\u800c\u7bad\u5934\u4e4b\u540e\u53ef\u4ee5\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff08\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff09\uff0c\u6216\u8005\u662f\u7528\u82b1\u62ec\u53f7\u62ec\u8d77\u7684\u51fd\u6570\u4f53\uff08\u9700\u8981\u81ea\u884c\u901a\u8fc7return\u6765\u8fd4\u56de\u503c\uff0c\u5426\u5219\u8fd4\u56de\u7684\u662fundefined\uff09\u3002<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n() =&gt; 1;\n(a, b) =&gt; a + b;\n() =&gt; ([1, 2]);\n() =&gt; ({\n    a: 1,\n    b: 2\n});\n() =&gt; {\n    alert()\n}\nsetTimeout(() =&gt; {\n    \/\/ to do\n}, 500)\n<\/pre>\n\n\n<h5 class=\"wp-block-heading\">\u5378\u8f7d\u76d1\u542c\u5668\u65f6\u7684\u9677\u9631<\/h5>\n\n\n\n<h6 class=\"wp-block-heading\">\u9519\u8bef\u7684\u505a\u6cd5<\/h6>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nclass PauseMenu extends React.Component {\n    componentWillMount() {\n        AppStateIOS.addEventListener('change', this.onAppPaused.bind(this));\n    }\n    componentWillUnmount(){\n        AppStateIOS.removeEventListener('change', this.onAppPaused.bind(this));\n    }\n    onAppPaused(event){}\n}\n\n<\/pre>\n\n\n<h5 class=\"wp-block-heading\">\u6b63\u786e\u7684\u505a\u6cd5<\/h5>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nclass PauseMenu extends React.Component {\n    constructor(props) {\n        super(props)\n        this._onAppPaused = this.onAppPaused.bind(this)\n    }\n\n    componentWillMount() {\n        AppStateIOS.addEventListener('change', this._onAppPaused);\n    }\n    componentWillUnmount(){\n        AppStateIOS.removeEventListener('change', this._onAppPaused);\n    }\n    onAppPaused(event){}\n}\n\n<\/pre>\n\n\n<p>\u9664\u4e0a\u8ff0\u7684\u505a\u6cd5\u5916\uff0cbabel\u6700\u65b0\u7248\u672c\u5141\u8bb8\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<em><\/em><\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nclass PauseMenu extends React.Component {\n    constructor(props) {\n        super(props)\n    }\n\n    componentWillMount() {\n        AppStateIOS.addEventListener('change', this.onAppPaused);\n    }\n    componentWillUnmount(){\n        AppStateIOS.removeEventListener('change', this.onAppPaused);\n    }\n    onAppPaused = (event) =&gt; {}\n}\n\n<\/pre>\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a\u4e0d\u8bba\u662fbind\u8fd8\u662f\u7bad\u5934\u51fd\u6570\uff0c\u6bcf\u6b21\u88ab\u6267\u884c\u90fd\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5f15\u7528\uff0c\u56e0\u6b64\u5982\u679c\u4f60\u8fd8\u9700\u8981\u51fd\u6570\u7684\u5f15\u7528\u53bb\u505a\u4e00\u4e9b\u522b\u7684\u4e8b\u60c5\uff08\u8b6c\u5982\u5378\u8f7d\u76d1\u542c\u5668\uff09\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u81ea\u5df1\u4fdd\u5b58\u8fd9\u4e2a\u5f15\u7528\u3002 <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> 4.\u5b9a\u4e49\u51fd\u6570\u53c2\u6570\u9ed8\u8ba4\u503c <\/h2>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ ES5 \u7ed9\u51fd\u6570\u5b9a\u4e49\u53c2\u6570\u9ed8\u8ba4\u503c\nfunction foo(width, height) {\n    var w = width || 20;\n    var h = height || 50;\n    console.log(w, h)\n}\n \n\/\/ ES6\nfunction foo(width= 20, height = 50) {\n    console.log(width, height)\n}\n<\/pre>\n\n\n<p>\u8fd9\u6837\u5199\u4e00\u822c\u6ca1\u95ee\u9898\uff0c\u4f46\u5f53 \u53c2\u6570\u7684\u5e03\u5c14\u503c\u4e3afalse\u65f6\uff0c\u5c31\u4f1a\u6709\u95ee\u9898\u4e86\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u8fd9\u6837\u8c03\u7528foo\u51fd\u6570\uff1afoo(0,'');<\/p>\n\n\n\n<p>\u56e0\u4e3a 0\u7684\u5e03\u5c14\u503c\u4e3afalse\uff0c\u8fd9\u6837width\u7684\u53d6\u503c\u5c06\u662f20\u3002<br>\u6240\u4ee5\u8bf4\uff0c \u51fd\u6570\u53c2\u6570\u9ed8\u8ba4\u503c\u4e0d\u4ec5\u80fd\u4f7f\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u7b80\u6d01\u800c\u4e14\u80fd\u89c4\u907f\u4e00\u4e9b\u95ee\u9898\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"> 5.\u6a21\u677f\u5b57\u7b26\u4e32 <\/h2>\n\n\n\n<p>ES6\u652f\u6301 \u6a21\u677f\u5b57\u7b26\u4e32\uff0c\u4f7f\u5f97\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u66f4\u52a0\u7684\u7b80\u6d01\u3001\u76f4\u89c2\u3002\u5728ES6\u4e2d\u901a\u8fc7 ${}\u5c31\u53ef\u4ee5\u5b8c\u6210\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\uff0c\u53ea\u9700\u8981\u5c06\u53d8\u91cf\u653e\u5728\u5927\u62ec\u53f7\u4e4b\u4e2d\u3002<br>ES6\u53cd\u5f15\u53f7 `` \u505a\u591a\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u3002<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/\u4e0d\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\uff1a\nvar str = 'hello, ' + name + ', my name is ' + myName;\n\/\/\u4f7f\u7528\u6a21\u677f\u5b57\u7b26\u4e32\uff1a\nlet str = `hello, ${name}, my name is ${myName}`;\n\n\u53e6\u5916\uff1aincludes repeat\n\/\/ includes\uff1a\u5224\u65ad\u662f\u5426\u5305\u542b\u7136\u540e\u76f4\u63a5\u8fd4\u56de\u5e03\u5c14\u503c\nlet str = 'hahah';\nconsole.log(str.includes('y')); \/\/ false\n\/\/ repeat: \u83b7\u53d6\u5b57\u7b26\u4e32\u91cd\u590dn\u6b21\nlet s = 'he';\nconsole.log(s.repeat(3)); \/\/ 'hehehe'\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\"> 6.\u89e3\u6784\u8d4b\u503c <\/h2>\n\n\n\n<p>\u89e3\u6784\u8d4b\u503c\u8bed\u6cd5\u662fJavaScript\u7684\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u4ece\u6570\u7ec4\u6216\u8005\u5bf9\u8c61\u4e2d\u5feb\u901f\u63d0\u53d6\u503c\u8d4b\u7ed9\u5b9a\u4e49\u7684\u53d8\u91cf\u3002<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/\u83b7\u53d6\u6570\u7ec4\u4e2d\u7684\u503c\nvar foo = [&quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;];\nvar [one, two, three] = foo;\nvar [first, , , last] = foo;\nvar a, b; [a, b] = [1, 2];\n\/\/\u5982\u679c\u6ca1\u6709\u4ece\u6570\u7ec4\u4e2d\u7684\u83b7\u53d6\u5230\u503c\uff0c\u4f60\u53ef\u4ee5\u4e3a\u53d8\u91cf\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c\nvar a, b; [a = 5, b = 7] = [1];\n\/\/\u901a\u8fc7\u89e3\u6784\u8d4b\u503c\u53ef\u4ee5\u65b9\u4fbf\u7684\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c\nvar a = 1;var b = 3; [a, b] = [b, a];\n\/\/\u83b7\u53d6\u5bf9\u8c61\u4e2d\u7684\u503c\nconst student = {\n  name: 'Ming',\n  age: '18',\n  city: 'Shanghai'\n};\nconst {name,age,city} = student;\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\"> 7.\u5ef6\u5c55\u64cd\u4f5c\u7b26(Spread operator) <\/h2>\n\n\n\n<p>\u5ef6\u5c55\u64cd\u4f5c\u7b26...\u53ef\u4ee5\u5728\u51fd\u6570\u8c03\u7528\/\u6570\u7ec4\u6784\u9020\u65f6, \u5c06\u6570\u7ec4\u8868\u8fbe\u5f0f\u6216\u8005string\u5728\u8bed\u6cd5\u5c42\u9762\u5c55\u5f00\uff1b\u8fd8\u53ef\u4ee5\u5728\u6784\u9020\u5bf9\u8c61\u65f6, \u5c06\u5bf9\u8c61\u8868\u8fbe\u5f0f\u6309key-value\u7684\u65b9\u5f0f\u5c55\u5f00\u3002 <\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/1.\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4f7f\u7528\u5ef6\u5c55\u64cd\u4f5c\u7b26\nfunction sum(x, y, z) {\n  return x + y + z\n}\nconst numbers = [1, 2, 3];\nconsole.log(sum.apply(null, numbers));\nconsole.log(sum(...numbers));\n\/\/2.\u6784\u9020\u6570\u7ec4\n\/\/\u6ca1\u6709\u5c55\u5f00\u8bed\u6cd5\u7684\u65f6\u5019\uff0c\u53ea\u80fd\u7ec4\u5408\u4f7f\u7528 push\uff0csplice\uff0cconcat \u7b49\u65b9\u6cd5\uff0c\u6765\u5c06\u5df2\u6709\u6570\u7ec4\u5143\u7d20\u53d8\u6210\u65b0\u6570\u7ec4\u7684\u4e00\u90e8\u5206\u3002\u6709\u4e86\u5c55\u5f00\u8bed\u6cd5, \u6784\u9020\u65b0\u6570\u7ec4\u4f1a\u53d8\u5f97\u66f4\u7b80\u5355\u3001\u66f4\u4f18\u96c5,\u548c\u53c2\u6570\u5217\u8868\u7684\u5c55\u5f00\u7c7b\u4f3c, ... \u5728\u6784\u9020\u5b57\u6570\u7ec4\u65f6, \u53ef\u4ee5\u5728\u4efb\u610f\u4f4d\u7f6e\u591a\u6b21\u4f7f\u7528\u3002\nconst stuendts = ['Jine', 'Tom'];\nconst persons = ['Tony', ...stuendts, 'Aaron', 'Anna'];\nconslog.log(persions)\n\/\/\u83b7\u53d6\u6570\u7ec4\u9664\u4e86\u67d0\u51e0\u9879\u7684\u5176\u4ed6\u9879\nlet num = [1, 3, 5, 7, 9];\nlet [first, second, ...rest] = num;\nconsole.log(rest); \/\/ [5, 7, 9]\n\/\/3.\u6570\u7ec4\u62f7\u8d1d\n\/\/\u5c55\u5f00\u8bed\u6cd5\u548c Object.assign() \u884c\u4e3a\u4e00\u81f4, \u6267\u884c\u7684\u90fd\u662f\u6d45\u62f7\u8d1d(\u53ea\u904d\u5386\u4e00\u5c42)\u3002\nvar arr = [1, 2, 3];\nvar arr2 = [...arr];\narr2.push(4);\nconsole.log(arr2)\n\/\/4.\u8fde\u63a5\u591a\u4e2a\u6570\u7ec4\nvar arr1 = [0, 1, 2];\nvar arr2 = [3, 4, 5];\nvar arr3 = [...arr1, ...arr2];\nvar arr4 = arr1.concat(arr2);\n\/\/5.\u5728ECMAScript 2018\u4e2d\u5ef6\u5c55\u64cd\u4f5c\u7b26\u589e\u52a0\u4e86\u5bf9\u5bf9\u8c61\u7684\u652f\u6301\nvar obj1 = {foo: 'bar',x: 42};\nvar obj2 = {foo: 'baz',y: 13};\nvar clonedObj = { ...obj1 };\nvar mergedObj = { ...obj1, ...obj2 };\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\"> 8.\u5bf9\u8c61\u5c5e\u6027\u7b80\u5199 <\/h2>\n\n\n\n<p> \u5728ES6\u4e2d\u5141\u8bb8\u6211\u4eec\u5728\u8bbe\u7f6e\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u7684\u65f6\u5019\u4e0d\u6307\u5b9a\u5c5e\u6027\u540d <\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/\u4e0d\u4f7f\u7528ES6\nconst name = 'Ming',\nage = '18',\ncity = 'Shanghai';\nconst student = {\n  name: name,\n  age: age,\n  city: city\n};\nconsole.log(student);\n\/\/\u4f7f\u7528ES6\nconst name = 'Ming',\nage = '18',\ncity = 'Shanghai';\nconst student = {\n  name,\n  age,\n  city\n};\nconsole.log(student);\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\"> 9.Promise <\/h2>\n\n\n\n<p>Promise \u662f\u5f02\u6b65\u7f16\u7a0b\u7684\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u6bd4\u4f20\u7edf\u7684\u89e3\u51b3\u65b9\u6848callback\u66f4\u52a0\u7684\u4f18\u96c5\u3002\u5b83\u6700\u65e9\u7531\u793e\u533a\u63d0\u51fa\u548c\u5b9e\u73b0\u7684\uff0cES6 \u5c06\u5176\u5199\u8fdb\u4e86\u8bed\u8a00\u6807\u51c6\uff0c\u7edf\u4e00\u4e86\u7528\u6cd5\uff0c\u539f\u751f\u63d0\u4f9b\u4e86Promise\u5bf9\u8c61\u3002<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/\u4e0d\u4f7f\u7528ES6,\u5d4c\u5957\u4e24\u4e2asetTimeout\u56de\u8c03\u51fd\u6570\nsetTimeout(function() {\n  console.log('Hello');\n  setTimeout(function() {\n    console.log('Hi')\n  },\n  1000)\n},\n1000);\n\/\/\u4f7f\u7528ES6\nvar waitSecond = new Promise(function(resolve, reject) {\n  setTimeout(resolve, 1000)\n});\nwaitSecond.then(function() {\n  console.log(&quot;Hello&quot;);\n  return waitSecond\n}).then(function() {\n  console.log(&quot;Hi&quot;)\n});\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\"> 10.\u652f\u6301let\u4e0econst <\/h2>\n\n\n\n<p>\u5728\u4e4b\u524dJS\u662f\u6ca1\u6709\u5757\u7ea7\u4f5c\u7528\u57df\u7684\uff0cconst\u4e0elet\u586b\u8865\u4e86\u8fd9\u65b9\u4fbf\u7684\u7a7a\u767d\uff0cconst\u4e0elet\u90fd\u662f\u5757\u7ea7\u4f5c\u7528\u57df\u3002 <\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/\u4f7f\u7528var\u5b9a\u4e49\u7684\u53d8\u91cf\u4e3a\u51fd\u6570\u7ea7\u4f5c\u7528\u57df\n{\n  var a = 10\n}\nconsole.log(a);\n\/\/\u4f7f\u7528let\u4e0econst\u5b9a\u4e49\u7684\u53d8\u91cf\u4e3a\u5757\u7ea7\u4f5c\u7528\u57df\n{\n  let a = 10\n}\nconsole.log(a); \/\/ \u62a5\u9519\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\"> <strong>11. Generators<\/strong> <\/h2>\n\n\n\n<p>\u751f\u6210\u5668\uff08 generator\uff09\u662f\u80fd\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u751f\u6210\u5668\u51fd\u6570\u4e5f\u662f\u4e00\u79cd\u51fd\u6570\uff0c\u6700\u76f4\u89c2\u7684\u8868\u73b0\u5c31\u662f\u6bd4\u666e\u901a\u7684function\u591a\u4e86\u4e2a\u661f\u53f7<code>*<\/code>\uff0c\u5728\u5176\u51fd\u6570\u4f53\u5185\u53ef\u4ee5\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57,\u6709\u610f\u601d\u7684\u662f\u51fd\u6570\u4f1a\u5728\u6bcf\u4e2a<code>yield<\/code>\u540e\u6682\u505c\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u751f\u6d3b\u4e2d\u6709\u4e00\u4e2a\u6bd4\u8f83\u5f62\u8c61\u7684\u4f8b\u5b50\u3002\u54b1\u4eec\u5230\u94f6\u884c\u529e\u7406\u4e1a\u52a1\u65f6\u5019\u90fd\u5f97\u5411\u5927\u5385\u7684\u673a\u5668\u53d6\u4e00\u5f20\u6392\u961f\u53f7\u3002\u4f60\u62ff\u5230\u4f60\u7684\u6392\u961f\u53f7\uff0c\u673a\u5668\u5e76\u4e0d\u4f1a\u81ea\u52a8\u4e3a\u4f60\u518d\u51fa\u4e0b\u4e00\u5f20\u7968\u3002\u4e5f\u5c31\u662f\u8bf4\u53d6\u7968\u673a\u201c\u6682\u505c\u201d\u4f4f\u4e86\uff0c\u76f4\u5230\u4e0b\u4e00\u4e2a\u4eba\u518d\u6b21\u5524\u8d77\u624d\u4f1a\u7ee7\u7eed\u5410\u7968\u3002<\/p>\n\n\n\n<p>\u8fed\u4ee3\u5668\uff1a\u5f53\u4f60\u8c03\u7528\u4e00\u4e2agenerator\u65f6\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002\u8fd9\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u62e5\u6709\u4e00\u4e2a\u53eb\u505anext\u7684\u65b9\u6cd5\u6765\u5e2e\u52a9\u4f60\u91cd\u542fgenerator\u51fd\u6570\u5e76\u5f97\u5230\u4e0b\u4e00\u4e2a\u503c\u3002next\u65b9\u6cd5\u4e0d\u4ec5\u8fd4\u56de\u503c\uff0c\u5b83\u8fd4\u56de\u7684\u5bf9\u8c61\u5177\u6709\u4e24\u4e2a\u5c5e\u6027\uff1adone\u548cvalue\u3002value\u662f\u4f60\u83b7\u5f97\u7684\u503c\uff0cdone\u7528\u6765\u8868\u660e\u4f60\u7684generator\u662f\u5426\u5df2\u7ecf\u505c\u6b62\u63d0\u4f9b\u503c\u3002\u7ee7\u7eed\u7528\u521a\u521a\u53d6\u7968\u7684\u4f8b\u5b50\uff0c\u6bcf\u5f20\u6392\u961f\u53f7\u5c31\u662f\u8fd9\u91cc\u7684value\uff0c\u6253\u5370\u7968\u7684\u7eb8\u662f\u5426\u7528\u5b8c\u5c31\u8fd9\u662f\u8fd9\u91cc\u7684done\u3002<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/ \u751f\u6210\u5668\nfunction *createIterator() {\nyield 1;\nyield 2;\nyield 3;\n}\n \n\/\/ \u751f\u6210\u5668\u80fd\u50cf\u6b63\u89c4\u51fd\u6570\u90a3\u6837\u88ab\u8c03\u7528\uff0c\u4f46\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\nlet iterator = createIterator();\n \nconsole.log(iterator.next().value); \/\/ 1\nconsole.log(iterator.next().value); \/\/ 2\nconsole.log(iterator.next().value); \/\/ 3\n<\/pre>\n\n\n<p>\u8fed\u4ee3\u5668\u5bf9\u5f02\u6b65\u7f16\u7a0b\u4f5c\u7528\u5f88\u5927\uff0c\u5f02\u6b65\u8c03\u7528\u5bf9\u4e8e\u6211\u4eec\u6765\u8bf4\u662f\u5f88\u56f0\u96be\u7684\u4e8b\uff0c\u6211\u4eec\u7684\u51fd\u6570\u5e76\u4e0d\u4f1a\u7b49\u5f85\u5f02\u6b65\u8c03\u7528\u5b8c\u518d\u6267\u884c\uff0c\u4f60\u53ef\u80fd\u4f1a\u60f3\u5230\u7528\u56de\u8c03\u51fd\u6570\uff0c\uff08\u5f53\u7136\u8fd8\u6709\u5176\u4ed6\u65b9\u6848\u6bd4\u5982Promise\u6bd4\u5982Async\/await\uff09\u3002<\/p>\n\n\n\n<p>\u751f\u6210\u5668\u53ef\u4ee5\u8ba9\u6211\u4eec\u7684\u4ee3\u7801\u8fdb\u884c\u7b49\u5f85\u3002\u5c31\u4e0d\u7528\u5d4c\u5957\u7684\u56de\u8c03\u51fd\u6570\u3002\u4f7f\u7528generator\u53ef\u4ee5\u786e\u4fdd\u5f53\u5f02\u6b65\u8c03\u7528\u5728\u6211\u4eec\u7684generator\u51fd\u6570\u8fd0\u884c\u4e00\u4e0b\u884c\u4ee3\u7801\u4e4b\u524d\u5b8c\u6210\u65f6\u6682\u505c\u51fd\u6570\u7684\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u54b1\u4eec\u4e5f\u4e0d\u80fd\u624b\u52a8\u4e00\u76f4\u8c03\u7528next()\u65b9\u6cd5\uff0c\u4f60\u9700\u8981\u4e00\u4e2a\u80fd\u591f\u8c03\u7528\u751f\u6210\u5668\u5e76\u542f\u52a8\u8fed\u4ee3\u5668\u7684\u65b9\u6cd5\u3002\u5c31\u50cf\u8fd9\u6837\u5b50\u7684\uff1a<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nfunction run(taskDef) {\n\u3000\u3000\/\/ taskDef \u5373\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\n\u3000\u3000\/\/ \u521b\u5efa\u8fed\u4ee3\u5668\uff0c\u8ba9\u5b83\u5728\u522b\u5904\u53ef\u7528\n\u3000\u3000let task = taskDef();\n \n\u3000\u3000\/\/ \u542f\u52a8\u4efb\u52a1\n\u3000\u3000let result = task.next();\n \n\u3000\u3000\/\/ \u9012\u5f52\u4f7f\u7528\u51fd\u6570\u6765\u4fdd\u6301\u5bf9 next() \u7684\u8c03\u7528\n\u3000\u3000function step() {\n\u3000\u3000\u3000\u3000\/\/ \u5982\u679c\u8fd8\u6709\u66f4\u591a\u8981\u505a\u7684\n\u3000\u3000\u3000\u3000if (!result.done) {\n\u3000\u3000\u3000\u3000\u3000\u3000result = task.next();\n\u3000\u3000\u3000\u3000\u3000\u3000step();\n\u3000\u3000\u3000\u3000}\n\u3000\u3000}\n \n\u3000\u3000\/\/ \u5f00\u59cb\u5904\u7406\u8fc7\u7a0b\n\u3000\u3000step();\n}\n<\/pre><!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"ES6 \u7b80\u4ecb ECMAScript 6 \u7b80\u79f0 ES6\uff0c\u662f JavaScript \u8bed\u8a00\u7684\u4e0b\u4e00\u4ee3\u6807\u51c6\uff0c\u5df2\u7ecf\u57282015\u5e746\u6708\u6b63\u5f0f\u53d1\u5e03\u4e86\u3002\u5b83\u7684\u76ee\u6807\u662f\u4f7f\u5f97 JavaScript \u8bed\u8a00\u53ef\u4ee5\u7528\u6765\u7f16\u5199\u590d\u6742\u7684\u5927\u578b\u5e94\u7528\u7a0b \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[],"table_tags":[],"_links":{"self":[{"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/posts\/784"}],"collection":[{"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/comments?post=784"}],"version-history":[{"count":2,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/posts\/784\/revisions"}],"predecessor-version":[{"id":787,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/posts\/784\/revisions\/787"}],"wp:attachment":[{"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/media?parent=784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/categories?post=784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/tags?post=784"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/table_tags?post=784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}