text: (window.webpackJsonp=window.webpackJsonp||[]).push([[80],{415:function(s,a,n){"use strict";n.r(a);var e=n(2),t=Object(e.a)({},(function(){var s=this,a=s.$createElement,n=s._self._c||a;return n("ContentSlotsDistributor",{attrs:{"slot-key":s.$parent.slotKey}},[n("h1",{attrs:{id:"es6、es7、es8、es9、es10新特性一览"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#es6、es7、es8、es9、es10新特性一览"}},[s._v("#")]),s._v(" ES6、ES7、ES8、ES9、ES10新特性一览")]),s._v(" "),n("blockquote",[n("p",[s._v("ES全称ECMAScript，ECMAScript是ECMA制定的标准化脚本语言。目前JavaScript使用的ECMAScript版本为"),n("a",{attrs:{href:"https://ecma-international.org/publications/standards/Ecma-417.htm",target:"_blank",rel:"noopener noreferrer"}},[s._v("ECMA-417"),n("OutboundLink")],1),s._v("。关于ECMA的最新资讯可以浏览 "),n("a",{attrs:{href:"https://www.ecma-international.org/news/index.html",target:"_blank",rel:"noopener noreferrer"}},[s._v("ECMA news"),n("OutboundLink")],1),s._v("查看。")])]),s._v(" "),n("p",[s._v("ECMA规范最终由"),n("a",{attrs:{href:"https://github.com/tc39",target:"_blank",rel:"noopener noreferrer"}},[s._v("TC39"),n("OutboundLink")],1),s._v("敲定。TC39由包括浏览器厂商在内的各方组成，他们开会推动JavaScript提案沿着一条严格的发展道路前进。 从提案到入选ECMA规范主要有以下几个阶段：")]),s._v(" "),n("ul",[n("li",[s._v("Stage 0: strawman——最初想法的提交。")]),s._v(" "),n("li",[s._v("Stage 1: proposal（提案）——由TC39至少一名成员倡导的正式提案文件，该文件包括API事例。")]),s._v(" "),n("li",[s._v("Stage 2: draft（草案）——功能规范的初始版本，该版本包含功能规范的两个实验实现。")]),s._v(" "),n("li",[s._v("Stage 3: candidate（候选）——提案规范通过审查并从厂商那里收集反馈")]),s._v(" "),n("li",[s._v("Stage 4: finished（完成）——提案准备加入ECMAScript，但是到浏览器或者Nodejs中可能需要更长的时间。")])]),s._v(" "),n("h2",{attrs:{id:"es6新特性（2015）"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#es6新特性（2015）"}},[s._v("#")]),s._v(" ES6新特性（2015）")]),s._v(" "),n("p",[s._v("ES6的特性比较多，在 ES5 发布近 6 年（2009-11 至 2015-6）之后才将其标准化。两个发布版本之间时间跨度很大，所以ES6中的特性比较多。 在这里列举几个常用的：")]),s._v(" "),n("ul",[n("li",[s._v("类")]),s._v(" "),n("li",[s._v("模块化")]),s._v(" "),n("li",[s._v("箭头函数")]),s._v(" "),n("li",[s._v("函数参数默认值")]),s._v(" "),n("li",[s._v("模板字符串")]),s._v(" "),n("li",[s._v("解构赋值")]),s._v(" "),n("li",[s._v("延展操作符")]),s._v(" "),n("li",[s._v("对象属性简写")]),s._v(" "),n("li",[s._v("Promise")]),s._v(" "),n("li",[s._v("Let与Const")])]),s._v(" "),n("h3",{attrs:{id:"_1-类（class）"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-类（class）"}},[s._v("#")]),s._v(" 1.类（class）")]),s._v(" "),n("p",[s._v("对熟悉Java，object-c，c#等纯面向对象语言的开发者来说，都会对class有一种特殊的情怀。ES6 引入了class（类），让JavaScript的面向对象编程变得更加简单和易于理解。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("  class Animal {\n    // 构造函数，实例化的时候将会被调用，如果不指定，那么会有一个不带参数的默认构造函数.\n    constructor(name,color) {\n      this.name = name;\n      this.color = color;\n    }\n    // toString 是原型对象上的属性\n    toString() {\n      console.log('name:' + this.name + ',color:' + this.color);\n\n    }\n  }\n\n var animal = new Animal('dog','white');//实例化Animal\n animal.toString();\n\n console.log(animal.hasOwnProperty('name')); //true\n console.log(animal.hasOwnProperty('toString')); // false\n console.log(animal.__proto__.hasOwnProperty('toString')); // true\n\n class Cat extends Animal {\n  constructor(action) {\n    // 子类必须要在constructor中指定super 函数，否则在新建实例的时候会报错.\n    // 如果没有置顶consructor,默认带super函数的constructor将会被添加、\n    super('cat','white');\n    this.action = action;\n  }\n  toString() {\n    console.log(super.toString());\n  }\n }\n\n var cat = new Cat('catch')\n cat.toString();\n\n // 实例cat 是 Cat 和 Animal 的实例，和Es5完全一致。\n console.log(cat instanceof Cat); // true\n console.log(cat instanceof Animal); // true\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br"),n("span",{staticClass:"line-number"},[s._v("16")]),n("br"),n("span",{staticClass:"line-number"},[s._v("17")]),n("br"),n("span",{staticClass:"line-number"},[s._v("18")]),n("br"),n("span",{staticClass:"line-number"},[s._v("19")]),n("br"),n("span",{staticClass:"line-number"},[s._v("20")]),n("br"),n("span",{staticClass:"line-number"},[s._v("21")]),n("br"),n("span",{staticClass:"line-number"},[s._v("22")]),n("br"),n("span",{staticClass:"line-number"},[s._v("23")]),n("br"),n("span",{staticClass:"line-number"},[s._v("24")]),n("br"),n("span",{staticClass:"line-number"},[s._v("25")]),n("br"),n("span",{staticClass:"line-number"},[s._v("26")]),n("br"),n("span",{staticClass:"line-number"},[s._v("27")]),n("br"),n("span",{staticClass:"line-number"},[s._v("28")]),n("br"),n("span",{staticClass:"line-number"},[s._v("29")]),n("br"),n("span",{staticClass:"line-number"},[s._v("30")]),n("br"),n("span",{staticClass:"line-number"},[s._v("31")]),n("br"),n("span",{staticClass:"line-number"},[s._v("32")]),n("br"),n("span",{staticClass:"line-number"},[s._v("33")]),n("br"),n("span",{staticClass:"line-number"},[s._v("34")]),n("br"),n("span",{staticClass:"line-number"},[s._v("35")]),n("br"),n("span",{staticClass:"line-number"},[s._v("36")]),n("br"),n("span",{staticClass:"line-number"},[s._v("37")]),n("br"),n("span",{staticClass:"line-number"},[s._v("38")]),n("br"),n("span",{staticClass:"line-number"},[s._v("39")]),n("br"),n("span",{staticClass:"line-number"},[s._v("40")]),n("br")])]),n("h3",{attrs:{id:"_2-模块化-module"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-模块化-module"}},[s._v("#")]),s._v(" 2.模块化(Module)")]),s._v(" "),n("p",[s._v("ES5不支持原生的模块化，在ES6中模块作为重要的组成部分被添加进来。模块的功能主要由 export 和 import 组成。每一个模块都有自己单独的作用域，模块之间的相互调用关系是通过 export 来规定模块对外暴露的接口，通过import来引用其它模块提供的接口。同时还为模块创造了命名空间，防止函数的命名冲突。")]),s._v(" "),n("h4",{attrs:{id:"导出-export"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#导出-export"}},[s._v("#")]),s._v(" 导出(export)")]),s._v(" "),n("p",[s._v("ES6允许在一个模块中使用export来导出多个变量或函数。")]),s._v(" "),n("p",[n("strong",[s._v("导出变量")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("//test.js\nexport var name = 'Rainbow'\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br")])]),n("blockquote",[n("p",[s._v("心得：ES6不仅支持变量的导出，也支持常量的导出。 "),n("code",[s._v("export const sqrt = Math.sqrt;//导出常量")])])]),s._v(" "),n("p",[s._v("ES6将一个文件视为一个模块，上面的模块通过 export 向外输出了一个变量。一个模块也可以同时往外面输出多个变量。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v(" //test.js\n var name = 'Rainbow';\n var age = '24';\n export {name, age};\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("p",[n("strong",[s._v("导出函数")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("// myModule.js\nexport function myModule(someArg) {\n  return someArg;\n}  \n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("h4",{attrs:{id:"导入-import"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#导入-import"}},[s._v("#")]),s._v(" 导入(import)")]),s._v(" "),n("p",[s._v("定义好模块的输出以后就可以在另外一个模块通过import引用。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("import {myModule} from 'myModule';// main.js\nimport {name,age} from 'test';// test.js\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br")])]),n("blockquote",[n("p",[s._v("心得:一条import 语句可以同时导入默认函数和其它变量。"),n("code",[s._v("import defaultMethod, { otherMethod } from 'xxx.js';")])])]),s._v(" "),n("h3",{attrs:{id:"_3-箭头（arrow）函数"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-箭头（arrow）函数"}},[s._v("#")]),s._v(" 3.箭头（Arrow）函数")]),s._v(" "),n("p",[s._v("这是ES6中最令人激动的特性之一。"),n("code",[s._v("=>")]),s._v("不只是关键字function的简写，它还带来了其它好处。箭头函数与包围它的代码共享同一个"),n("code",[s._v("this")]),s._v(",能帮你很好的解决this的指向问题。有经验的JavaScript开发者都熟悉诸如"),n("code",[s._v("var self = this;")]),s._v("或"),n("code",[s._v("var that = this")]),s._v("这种引用外围this的模式。但借助"),n("code",[s._v("=>")]),s._v("，就不需要这种模式了。")]),s._v(" "),n("h4",{attrs:{id:"箭头函数的结构"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#箭头函数的结构"}},[s._v("#")]),s._v(" 箭头函数的结构")]),s._v(" "),n("p",[s._v("箭头函数的箭头=>之前是一个空括号、单个的参数名、或用括号括起的多个参数名，而箭头之后可以是一个表达式（作为函数的返回值），或者是用花括号括起的函数体（需要自行通过return来返回值，否则返回的是undefined）。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v('// 箭头函数的例子\n()=>1\nv=>v+1\n(a,b)=>a+b\n()=>{\n    alert("foo");\n}\ne=>{\n    if (e == 0){\n        return 0;\n    }\n    return 1000/e;\n}\n\n复制代码\n')])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br")])]),n("blockquote",[n("p",[s._v("心得：不论是箭头函数还是bind，每次被执行都返回的是一个新的函数引用，因此如果你还需要函数的引用去做一些别的事情（譬如卸载监听器），那么你必须自己保存这个引用。")])]),s._v(" "),n("h4",{attrs:{id:"卸载监听器时的陷阱"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#卸载监听器时的陷阱"}},[s._v("#")]),s._v(" 卸载监听器时的陷阱")]),s._v(" "),n("blockquote",[n("p",[n("strong",[s._v("错误的做法")])])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("class 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\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br")])]),n("blockquote",[n("p",[n("strong",[s._v("正确的做法")])])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("class PauseMenu extends React.Component{\n    constructor(props){\n        super(props);\n        this._onAppPaused = this.onAppPaused.bind(this);\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\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br"),n("span",{staticClass:"line-number"},[s._v("16")]),n("br")])]),n("p",[s._v("除上述的做法外，我们还可以这样做：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("class PauseMenu extends React.Component{\n    componentWillMount(){\n        AppStateIOS.addEventListener('change', this.onAppPaused);\n    }\n    componentWillUnmount(){\n        AppStateIOS.removeEventListener('change', this.onAppPaused);\n    }\n    onAppPaused = (event) => {\n        //把函数直接作为一个arrow function的属性来定义，初始化的时候就绑定好了this指针\n    }\n}\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br")])]),n("blockquote",[n("p",[s._v("需要注意的是：不论是bind还是箭头函数，每次被执行都返回的是一个新的函数引用，因此如果你还需要函数的引用去做一些别的事情（譬如卸载监听器），那么你必须自己保存这个引用。")])]),s._v(" "),n("h3",{attrs:{id:"_4-函数参数默认值"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-函数参数默认值"}},[s._v("#")]),s._v(" 4."),n("a",{attrs:{href:"https://link.juejin.im/?target=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FFunctions%2Fdefault_parameters",target:"_blank",rel:"noopener noreferrer"}},[s._v("函数参数默认值"),n("OutboundLink")],1)]),s._v(" "),n("p",[s._v("ES6支持在定义函数的时候为其设置默认值：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("function foo(height = 50, color = 'red')\n{\n    // ...\n}\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("blockquote",[n("p",[s._v("不使用默认值：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("function foo(height, color)\n{\n    var height = height || 50;\n    var color = color || 'red';\n    //...\n}\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br")])]),n("p",[s._v("这样写一般没问题，但当"),n("code",[s._v("参数的布尔值为false")]),s._v("时，就会有问题了。比如，我们这样调用foo函数：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v('foo(0, "")\n\n复制代码\n')])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("p",[s._v("因为"),n("code",[s._v("0的布尔值为false")]),s._v("，这样height的取值将是50。同理color的取值为‘red’。")]),s._v(" "),n("p",[s._v("所以说，"),n("code",[s._v("函数参数默认值")]),s._v("不仅能是代码变得更加简洁而且能规避一些问题。")]),s._v(" "),n("h3",{attrs:{id:"_5-模板字符串"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-模板字符串"}},[s._v("#")]),s._v(" 5.模板字符串")]),s._v(" "),n("p",[s._v("ES6支持"),n("code",[s._v("模板字符串")]),s._v("，使得字符串的拼接更加的简洁、直观。")]),s._v(" "),n("blockquote",[n("p",[s._v("不使用模板字符串：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var name = 'Your name is ' + first + ' ' + last + '.'\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("blockquote",[n("p",[s._v("使用模板字符串：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var name = `Your name is ${first} ${last}.`\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("p",[s._v("在ES6中通过"),n("code",[s._v("${}")]),s._v("就可以完成字符串的拼接，只需要将变量放在大括号之中。")]),s._v(" "),n("h3",{attrs:{id:"_6-解构赋值"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_6-解构赋值"}},[s._v("#")]),s._v(" 6."),n("a",{attrs:{href:"https://link.juejin.im/?target=https%3A%2F%2Fdeveloper.mozilla.org%2Fen-US%2Fdocs%2FWeb%2FJavaScript%2FReference%2FOperators%2FDestructuring_assignment",target:"_blank",rel:"noopener noreferrer"}},[s._v("解构赋值"),n("OutboundLink")],1)]),s._v(" "),n("p",[s._v("解构赋值语法是JavaScript的一种表达式，可以方便的从数组或者对象中快速提取值赋给定义的变量。")]),s._v(" "),n("h4",{attrs:{id:"获取数组中的值"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#获取数组中的值"}},[s._v("#")]),s._v(" 获取数组中的值")]),s._v(" "),n("p",[s._v("从数组中获取值并赋值到变量中，变量的顺序与数组中对象顺序对应。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v('var foo = ["one", "two", "three", "four"];\n\nvar [one, two, three] = foo;\nconsole.log(one); // "one"\nconsole.log(two); // "two"\nconsole.log(three); // "three"\n\n//如果你要忽略某些值，你可以按照下面的写法获取你想要的值\nvar [first, , , last] = foo;\nconsole.log(first); // "one"\nconsole.log(last); // "four"\n\n//你也可以这样写\nvar a, b; //先声明变量\n\n[a, b] = [1, 2];\nconsole.log(a); // 1\nconsole.log(b); // 2\n\n复制代码\n')])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br"),n("span",{staticClass:"line-number"},[s._v("16")]),n("br"),n("span",{staticClass:"line-number"},[s._v("17")]),n("br"),n("span",{staticClass:"line-number"},[s._v("18")]),n("br"),n("span",{staticClass:"line-number"},[s._v("19")]),n("br"),n("span",{staticClass:"line-number"},[s._v("20")]),n("br")])]),n("p",[s._v("如果没有从数组中的获取到值，你可以为变量设置一个默认值。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var a, b;\n\n[a=5, b=7] = [1];\nconsole.log(a); // 1\nconsole.log(b); // 7\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("p",[s._v("通过解构赋值可以方便的交换两个变量的值。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var a = 1;\nvar b = 3;\n\n[a, b] = [b, a];\nconsole.log(a); // 3\nconsole.log(b); // 1\n\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br")])]),n("h4",{attrs:{id:"获取对象中的值"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#获取对象中的值"}},[s._v("#")]),s._v(" 获取对象中的值")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const student = {\n  name:'Ming',\n  age:'18',\n  city:'Shanghai'  \n};\n\nconst {name,age,city} = student;\nconsole.log(name); // \"Ming\"\nconsole.log(age); // \"18\"\nconsole.log(city); // \"Shanghai\"\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br")])]),n("h3",{attrs:{id:"_7-延展操作符-spread-operator"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_7-延展操作符-spread-operator"}},[s._v("#")]),s._v(" 7.延展操作符(Spread operator)")]),s._v(" "),n("p",[n("code",[s._v("延展操作符...")]),s._v("可以在函数调用/数组构造时, 将数组表达式或者string在语法层面展开；还可以在构造对象时, 将对象表达式按key-value的方式展开。")]),s._v(" "),n("h4",{attrs:{id:"语法"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#语法"}},[s._v("#")]),s._v(" 语法")]),s._v(" "),n("blockquote",[n("p",[s._v("函数调用：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("myFunction(...iterableObj);\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("blockquote",[n("p",[s._v("数组构造或字符串：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("[...iterableObj, '4', ...'hello', 6];\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("blockquote",[n("p",[s._v("构造对象时,进行克隆或者属性拷贝（ECMAScript 2018规范新增特性）：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("let objClone = { ...obj };\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("h4",{attrs:{id:"应用场景"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#应用场景"}},[s._v("#")]),s._v(" 应用场景")]),s._v(" "),n("blockquote",[n("p",[s._v("在函数调用时使用延展操作符")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("function sum(x, y, z) {\n  return x + y + z;\n}\nconst numbers = [1, 2, 3];\n\n//不使用延展操作符\nconsole.log(sum.apply(null, numbers));\n\n//使用延展操作符\nconsole.log(sum(...numbers));// 6\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br")])]),n("blockquote",[n("p",[s._v("构造数组")])]),s._v(" "),n("p",[s._v("没有展开语法的时候，只能组合使用 push，splice，concat 等方法，来将已有数组元素变成新数组的一部分。有了展开语法, 构造新数组会变得更简单、更优雅：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const stuendts = ['Jine','Tom']; \nconst persons = ['Tony',... stuendts,'Aaron','Anna'];\nconslog.log(persions)// [\"Tony\", \"Jine\", \"Tom\", \"Aaron\", \"Anna\"]\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br")])]),n("p",[s._v("和参数列表的展开类似, "),n("code",[s._v("...")]),s._v(" 在构造字数组时, 可以在任意位置多次使用。")]),s._v(" "),n("blockquote",[n("p",[s._v("数组拷贝")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var arr = [1, 2, 3];\nvar arr2 = [...arr]; // 等同于 arr.slice()\narr2.push(4); \nconsole.log(arr2)//[1, 2, 3, 4]\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("p",[s._v("展开语法和 Object.assign() 行为一致, 执行的都是浅拷贝(只遍历一层)。")]),s._v(" "),n("blockquote",[n("p",[s._v("连接多个数组")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var arr1 = [0, 1, 2];\nvar arr2 = [3, 4, 5];\nvar arr3 = [...arr1, ...arr2];// 将 arr2 中所有元素附加到 arr1 后面并返回\n//等同于\nvar arr4 = arr1.concat(arr2);\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("h4",{attrs:{id:"在ecmascript-2018中延展操作符增加了对对象的支持"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#在ecmascript-2018中延展操作符增加了对对象的支持"}},[s._v("#")]),s._v(" 在ECMAScript 2018中延展操作符增加了对对象的支持")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var obj1 = { foo: 'bar', x: 42 };\nvar obj2 = { foo: 'baz', y: 13 };\n\nvar clonedObj = { ...obj1 };\n// 克隆后的对象: { foo: \"bar\", x: 42 }\n\nvar mergedObj = { ...obj1, ...obj2 };\n// 合并后的对象: { foo: \"baz\", x: 42, y: 13 }\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br")])]),n("h4",{attrs:{id:"在react中的应用"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#在react中的应用"}},[s._v("#")]),s._v(" 在React中的应用")]),s._v(" "),n("p",[s._v("通常我们在封装一个组件时，会对外公开一些 props 用于实现功能。大部分情况下在外部使用都应显示的传递 props 。但是当传递大量的props时，会非常繁琐，这时我们可以使用 "),n("code",[s._v("...(延展操作符,用于取出参数对象的所有可遍历属性)")]),s._v(" 来进行传递。")]),s._v(" "),n("h4",{attrs:{id:"一般情况下我们应该这样写"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#一般情况下我们应该这样写"}},[s._v("#")]),s._v(" 一般情况下我们应该这样写")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("<CustomComponent name ='Jine' age ={21} />\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("blockquote",[n("p",[s._v("使用 ... ，等同于上面的写法")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const params = {\n\tname: 'Jine',\n\tage: 21\n}\n<CustomComponent {...params} />\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("blockquote",[n("p",[s._v("配合解构赋值避免传入一些不需要的参数")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var params = {\n\tname: '123',\n\ttitle: '456',\n\ttype: 'aaa'\n}\n\nvar { type, ...other } = params;\n\n<CustomComponent type='normal' number={2} {...other} />\n//等同于\n<CustomComponent type='normal' number={2} name='123' title='456' />\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br")])]),n("h3",{attrs:{id:"_8-对象属性简写"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_8-对象属性简写"}},[s._v("#")]),s._v(" 8.对象属性简写")]),s._v(" "),n("p",[s._v("在ES6中允许我们在设置一个对象的属性的时候不指定属性名。")]),s._v(" "),n("blockquote",[n("p",[s._v("不使用ES6")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const name='Ming',age='18',city='Shanghai';\n   \nconst student = {\n    name:name,\n    age:age,\n    city:city\n};\nconsole.log(student);//{name: \"Ming\", age: \"18\", city: \"Shanghai\"}\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br")])]),n("p",[s._v("对象中必须包含属性和值，显得非常冗余。")]),s._v(" "),n("blockquote",[n("p",[s._v("使用ES6")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const name='Ming',age='18',city='Shanghai';\n  \nconst student = {\n    name,\n    age,\n    city\n};\nconsole.log(student);//{name: \"Ming\", age: \"18\", city: \"Shanghai\"}\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br")])]),n("p",[s._v("对象中直接写变量，非常简洁。")]),s._v(" "),n("h3",{attrs:{id:"_9-promise"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_9-promise"}},[s._v("#")]),s._v(" 9."),n("a",{attrs:{href:"https://link.juejin.im/?target=https%3A%2F%2Fdeveloper.mozilla.org%2Fzh-CN%2Fdocs%2FWeb%2FJavaScript%2FGuide%2FUsing_promises",target:"_blank",rel:"noopener noreferrer"}},[s._v("Promise"),n("OutboundLink")],1)]),s._v(" "),n("p",[s._v("Promise 是异步编程的一种解决方案，比传统的解决方案callback更加的优雅。它最早由社区提出和实现的，ES6 将其写进了语言标准，统一了用法，原生提供了Promise对象。")]),s._v(" "),n("blockquote",[n("p",[s._v("不使用ES6")])]),s._v(" "),n("p",[s._v("嵌套两个setTimeout回调函数：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("setTimeout(function()\n{\n    console.log('Hello'); // 1秒后输出\"Hello\"\n    setTimeout(function()\n    {\n        console.log('Hi'); // 2秒后输出\"Hi\"\n    }, 1000);\n}, 1000);\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br")])]),n("blockquote",[n("p",[s._v("使用ES6")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v('var waitSecond = new Promise(function(resolve, reject)\n{\n    setTimeout(resolve, 1000);\n});\n\nwaitSecond\n    .then(function()\n    {\n      console.log("Hello"); // 1秒后输出"Hello"\n      return waitSecond;\n    })\n    .then(function()\n    {\n        console.log("Hi"); // 2秒后输出"Hi"\n    });\n\n复制代码\n')])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br"),n("span",{staticClass:"line-number"},[s._v("16")]),n("br"),n("span",{staticClass:"line-number"},[s._v("17")]),n("br")])]),n("p",[s._v("上面的的代码使用两个then来进行异步编程串行化，避免了回调地狱：")]),s._v(" "),n("h3",{attrs:{id:"_10-支持let与const"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_10-支持let与const"}},[s._v("#")]),s._v(" 10.支持let与const")]),s._v(" "),n("p",[s._v("在之前JS是没有块级作用域的，const与let填补了这方便的空白，const与let都是块级作用域。")]),s._v(" "),n("blockquote",[n("p",[s._v("使用var定义的变量为函数级作用域：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("{\n  var a = 10;\n}\n\nconsole.log(a); // 输出10\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("blockquote",[n("p",[s._v("使用let与const定义的变量为块级作用域：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("{\n  let a = 10;\n}\n\nconsole.log(a); //-1 or Error“ReferenceError: a is not defined”\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("h2",{attrs:{id:"es7新特性（2016）"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#es7新特性（2016）"}},[s._v("#")]),s._v(" ES7新特性（2016）")]),s._v(" "),n("p",[s._v("ES2016添加了两个小的特性来说明标准化过程：")]),s._v(" "),n("ul",[n("li",[s._v("数组includes()方法，用来判断一个数组是否包含一个指定的值，根据情况，如果包含则返回true，否则返回false。")]),s._v(" "),n("li",[s._v("a ** b指数运算符，它与 Math.pow(a, b)相同。")])]),s._v(" "),n("h3",{attrs:{id:"_1-array-prototype-includes"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-array-prototype-includes"}},[s._v("#")]),s._v(" 1.Array.prototype.includes()")]),s._v(" "),n("p",[n("code",[s._v("includes()")]),s._v(" 函数用来判断一个数组是否包含一个指定的值，如果包含则返回 "),n("code",[s._v("true")]),s._v("，否则返回"),n("code",[s._v("false")]),s._v("。")]),s._v(" "),n("p",[n("code",[s._v("includes")]),s._v(" 函数与 "),n("code",[s._v("indexOf")]),s._v(" 函数很相似，下面两个表达式是等价的：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("arr.includes(x)\narr.indexOf(x) >= 0\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br")])]),n("p",[s._v("接下来我们来判断数字中是否包含某个元素：")]),s._v(" "),n("blockquote",[n("p",[s._v("在ES7之前的做法")])]),s._v(" "),n("p",[s._v("使用"),n("code",[s._v("indexOf()")]),s._v("验证数组中是否存在某个元素，这时需要根据返回值是否为-1来判断：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("let arr = ['react', 'angular', 'vue'];\n\nif (arr.indexOf('react') !== -1)\n{\n    console.log('react存在');\n}\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br")])]),n("blockquote",[n("p",[s._v("使用ES7的includes()")])]),s._v(" "),n("p",[s._v("使用includes()验证数组中是否存在某个元素，这样更加直观简单：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("let arr = ['react', 'angular', 'vue'];\n\nif (arr.includes('react'))\n{\n    console.log('react存在');\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("h3",{attrs:{id:"_2-指数操作符"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-指数操作符"}},[s._v("#")]),s._v(" 2.指数操作符")]),s._v(" "),n("p",[s._v("在ES7中引入了指数运算符"),n("code",[s._v("**")]),s._v("，"),n("code",[s._v("**")]),s._v("具有与"),n("code",[s._v("Math.pow(..)")]),s._v("等效的计算结果。")]),s._v(" "),n("blockquote",[n("p",[s._v("不使用指数操作符")])]),s._v(" "),n("p",[s._v("使用自定义的递归函数calculateExponent或者Math.pow()进行指数运算：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("function calculateExponent(base, exponent)\n{\n    if (exponent === 1)\n    {\n        return base;\n    }\n    else\n    {\n        return base * calculateExponent(base, exponent - 1);\n    }\n}\n\nconsole.log(calculateExponent(2, 10)); // 输出1024\nconsole.log(Math.pow(2, 10)); // 输出1024\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br")])]),n("blockquote",[n("p",[s._v("使用指数操作符")])]),s._v(" "),n("p",[s._v("使用指数运算符**，就像+、-等操作符一样：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("console.log(2**10);// 输出1024\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br")])]),n("h2",{attrs:{id:"es8新特性（2017）"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#es8新特性（2017）"}},[s._v("#")]),s._v(" ES8新特性（2017）")]),s._v(" "),n("ul",[n("li",[s._v("async/await")]),s._v(" "),n("li",[n("code",[s._v("Object.values()")])]),s._v(" "),n("li",[n("code",[s._v("Object.entries()")])]),s._v(" "),n("li",[s._v("String padding: "),n("code",[s._v("padStart()")]),s._v("和"),n("code",[s._v("padEnd()")]),s._v("，填充字符串达到当前长度")]),s._v(" "),n("li",[s._v("函数参数列表结尾允许逗号")]),s._v(" "),n("li",[n("code",[s._v("Object.getOwnPropertyDescriptors()")])]),s._v(" "),n("li",[n("code",[s._v("ShareArrayBuffer")]),s._v("和"),n("code",[s._v("Atomics")]),s._v("对象，用于从共享内存位置读取和写入")])]),s._v(" "),n("h3",{attrs:{id:"_1-async-await"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-async-await"}},[s._v("#")]),s._v(" 1.async/await")]),s._v(" "),n("p",[s._v("ES2018引入异步迭代器（asynchronous iterators），这就像常规迭代器，除了"),n("code",[s._v("next()")]),s._v("方法返回一个Promise。因此"),n("code",[s._v("await")]),s._v("可以和"),n("code",[s._v("for...of")]),s._v("循环一起使用，以串行的方式运行异步操作。例如：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("async function process(array) {\n  for await (let i of array) {\n    doSomething(i);\n  }\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("h3",{attrs:{id:"_2-object-values"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-object-values"}},[s._v("#")]),s._v(" 2.Object.values()")]),s._v(" "),n("p",[n("code",[s._v("Object.values()")]),s._v("是一个与"),n("code",[s._v("Object.keys()")]),s._v("类似的新函数，但返回的是Object自身属性的所有值，不包括继承的值。")]),s._v(" "),n("p",[s._v("假设我们要遍历如下对象"),n("code",[s._v("obj")]),s._v("的所有值：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const obj = {a: 1, b: 2, c: 3};\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br")])]),n("blockquote",[n("p",[s._v("不使用Object.values() :ES7")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const vals=Object.keys(obj).map(key=>obj[key]);\nconsole.log(vals);//[1, 2, 3]\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("blockquote",[n("p",[s._v("使用Object.values() :ES8")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const values=Object.values(obj1);\nconsole.log(values);//[1, 2, 3]\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("p",[s._v("从上述代码中可以看出"),n("code",[s._v("Object.values()")]),s._v("为我们省去了遍历key，并根据这些key获取value的步骤。")]),s._v(" "),n("h3",{attrs:{id:"_3-object-entries"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-object-entries"}},[s._v("#")]),s._v(" 3.Object.entries()")]),s._v(" "),n("p",[n("code",[s._v("Object.entries()")]),s._v("函数返回一个给定对象自身可枚举属性的键值对的数组。")]),s._v(" "),n("p",[s._v("接下来我们来遍历上文中的"),n("code",[s._v("obj")]),s._v("对象的所有属性的key和value：")]),s._v(" "),n("blockquote",[n("p",[s._v("不使用Object.entries() :ES7")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("Object.keys(obj).forEach(key=>{\n\tconsole.log('key:'+key+' value:'+obj[key]);\n})\n//key:a value:1\n//key:b value:2\n//key:c value:3\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("blockquote",[n("p",[s._v("使用Object.entries() :ES8")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("for(let [key,value] of Object.entries(obj1)){\n\tconsole.log(`key: ${key} value:${value}`)\n}\n//key:a value:1\n//key:b value:2\n//key:c value:3\n\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br")])]),n("h3",{attrs:{id:"_4-string-padding"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-string-padding"}},[s._v("#")]),s._v(" 4.String padding")]),s._v(" "),n("p",[s._v("在ES8中String新增了两个实例函数"),n("code",[s._v("String.prototype.padStart")]),s._v("和"),n("code",[s._v("String.prototype.padEnd")]),s._v("，允许将空字符串或其他字符串添加到原始字符串的开头或结尾。")]),s._v(" "),n("blockquote",[n("p",[s._v("String.padStart(targetLength,[padString])")])]),s._v(" "),n("ul",[n("li",[s._v("targetLength:当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度，则返回当前字符串本身。")]),s._v(" "),n("li",[s._v('padString:(可选)填充字符串。如果字符串太长，使填充后的字符串长度超过了目标长度，则只保留最左侧的部分，其他部分会被截断，此参数的缺省值为 " "。')])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("console.log('0.0'.padStart(4,'10')) //10.0\nconsole.log('0.0'.padStart(20))// 0.00    \n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("blockquote",[n("p",[s._v("String.padEnd(targetLength,padString])")])]),s._v(" "),n("ul",[n("li",[s._v("targetLength:当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度，则返回当前字符串本身。")]),s._v(" "),n("li",[s._v('padString:(可选) 填充字符串。如果字符串太长，使填充后的字符串长度超过了目标长度，则只保留最左侧的部分，其他部分会被截断，此参数的缺省值为 " "；')])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("console.log('0.0'.padEnd(4,'0')) //0.00    \nconsole.log('0.0'.padEnd(10,'0'))//0.00000000\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("h3",{attrs:{id:"_5-函数参数列表结尾允许逗号"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-函数参数列表结尾允许逗号"}},[s._v("#")]),s._v(" 5.函数参数列表结尾允许逗号")]),s._v(" "),n("p",[s._v("主要作用是方便使用git进行多人协作开发时修改同一个函数减少不必要的行变更。")]),s._v(" "),n("h3",{attrs:{id:"_6-object-getownpropertydescriptors"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_6-object-getownpropertydescriptors"}},[s._v("#")]),s._v(" 6.Object.getOwnPropertyDescriptors()")]),s._v(" "),n("p",[n("code",[s._v("Object.getOwnPropertyDescriptors()")]),s._v("函数用来获取一个对象的所有自身属性的描述符,如果没有任何自身属性，则返回空对象。")]),s._v(" "),n("blockquote",[n("p",[s._v("函数原型：")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("Object.getOwnPropertyDescriptors(obj)\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br")])]),n("p",[s._v("返回"),n("code",[s._v("obj")]),s._v("对象的所有自身属性的描述符，如果没有任何自身属性，则返回空对象。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const obj2 = {\n\tname: 'Jine',\n\tget age() { return '18' }\n};\nObject.getOwnPropertyDescriptors(obj2)\n// {\n//   age: {\n//     configurable: true,\n//     enumerable: true,\n//     get: function age(){}, //the getter function\n//     set: undefined\n//   },\n//   name: {\n//     configurable: true,\n//     enumerable: true,\n//\t\tvalue:\"Jine\",\n//\t\twritable:true\n//   }\n// }\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br"),n("span",{staticClass:"line-number"},[s._v("16")]),n("br"),n("span",{staticClass:"line-number"},[s._v("17")]),n("br"),n("span",{staticClass:"line-number"},[s._v("18")]),n("br"),n("span",{staticClass:"line-number"},[s._v("19")]),n("br"),n("span",{staticClass:"line-number"},[s._v("20")]),n("br")])]),n("h3",{attrs:{id:"_7-sharedarraybuffer对象"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_7-sharedarraybuffer对象"}},[s._v("#")]),s._v(" 7.SharedArrayBuffer对象")]),s._v(" "),n("p",[s._v("SharedArrayBuffer 对象用来表示一个通用的，固定长度的原始二进制数据缓冲区，类似于 ArrayBuffer 对象，它们都可以用来在共享内存（shared memory）上创建视图。与 ArrayBuffer 不同的是，SharedArrayBuffer 不能被分离。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("/**\n * \n * @param {*} length 所创建的数组缓冲区的大小，以字节(byte)为单位。  \n * @returns {SharedArrayBuffer} 一个大小指定的新 SharedArrayBuffer 对象。其内容被初始化为 0。\n */\nnew SharedArrayBuffer(length)\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("h3",{attrs:{id:"_8-atomics对象"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_8-atomics对象"}},[s._v("#")]),s._v(" 8.Atomics对象")]),s._v(" "),n("p",[s._v("Atomics 对象提供了一组静态方法用来对 SharedArrayBuffer 对象进行原子操作。")]),s._v(" "),n("p",[s._v("这些原子操作属于 Atomics 模块。与一般的全局对象不同，Atomics 不是构造函数，因此不能使用 new 操作符调用，也不能将其当作函数直接调用。Atomics 的所有属性和方法都是静态的（与 Math 对象一样）。")]),s._v(" "),n("p",[s._v("多个共享内存的线程能够同时读写同一位置上的数据。原子操作会确保正在读或写的数据的值是符合预期的，即下一个原子操作一定会在上一个原子操作结束后才会开始，其操作过程不会中断。")]),s._v(" "),n("ul",[n("li",[s._v("Atomics.add()")])]),s._v(" "),n("blockquote",[n("p",[s._v("将指定位置上的数组元素与给定的值相加，并返回相加前该元素的值。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.and()")])]),s._v(" "),n("blockquote",[n("p",[s._v("将指定位置上的数组元素与给定的值相与，并返回与操作前该元素的值。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.compareExchange()")])]),s._v(" "),n("blockquote",[n("p",[s._v("如果数组中指定的元素与给定的值相等，则将其更新为新的值，并返回该元素原先的值。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.exchange()")])]),s._v(" "),n("blockquote",[n("p",[s._v("将数组中指定的元素更新为给定的值，并返回该元素更新前的值。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.load()")])]),s._v(" "),n("blockquote",[n("p",[s._v("返回数组中指定元素的值。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.or()")])]),s._v(" "),n("blockquote",[n("p",[s._v("将指定位置上的数组元素与给定的值相或，并返回或操作前该元素的值。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.store()")])]),s._v(" "),n("blockquote",[n("p",[s._v("将数组中指定的元素设置为给定的值，并返回该值。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.sub()")])]),s._v(" "),n("blockquote",[n("p",[s._v("将指定位置上的数组元素与给定的值相减，并返回相减前该元素的值。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.xor()")])]),s._v(" "),n("blockquote",[n("p",[s._v("将指定位置上的数组元素与给定的值相异或，并返回异或操作前该元素的值。")])]),s._v(" "),n("p",[s._v("wait() 和 wake() 方法采用的是 Linux 上的 futexes 模型（fast user-space mutex，快速用户空间互斥量），可以让进程一直等待直到某个特定的条件为真，主要用于实现阻塞。")]),s._v(" "),n("ul",[n("li",[s._v("Atomics.wait()")])]),s._v(" "),n("blockquote",[n("p",[s._v('检测数组中某个指定位置上的值是否仍然是给定值，是则保持挂起直到被唤醒或超时。返回值为 "ok"、"not-equal" 或 "time-out"。调用时，如果当前线程不允许阻塞，则会抛出异常（大多数浏览器都不允许在主线程中调用 wait()）。')])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.wake()")])]),s._v(" "),n("blockquote",[n("p",[s._v("唤醒等待队列中正在数组指定位置的元素上等待的线程。返回值为成功唤醒的线程数量。")])]),s._v(" "),n("ul",[n("li",[s._v("Atomics.isLockFree(size)")])]),s._v(" "),n("blockquote",[n("p",[s._v("可以用来检测当前系统是否支持硬件级的原子操作。对于指定大小的数组，如果当前系统支持硬件级的原子操作，则返回 true；否则就意味着对于该数组，Atomics 对象中的各原子操作都只能用锁来实现。此函数面向的是技术专家。--\x3e")])]),s._v(" "),n("h2",{attrs:{id:"es9新特性（2018）"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#es9新特性（2018）"}},[s._v("#")]),s._v(" ES9新特性（2018）")]),s._v(" "),n("ul",[n("li",[s._v("异步迭代")]),s._v(" "),n("li",[s._v("Promise.finally()")]),s._v(" "),n("li",[s._v("Rest/Spread 属性")]),s._v(" "),n("li",[n("a",{attrs:{href:"http://esnext.justjavac.com/proposal/regexp-named-groups.html",target:"_blank",rel:"noopener noreferrer"}},[s._v("正则表达式命名捕获组"),n("OutboundLink")],1),s._v("（Regular Expression Named Capture Groups）")]),s._v(" "),n("li",[n("a",{attrs:{href:"https://segmentfault.com/a/1190000006824133",target:"_blank",rel:"noopener noreferrer"}},[s._v("正则表达式反向断言"),n("OutboundLink")],1),s._v("（lookbehind）")]),s._v(" "),n("li",[s._v("正则表达式dotAll模式")]),s._v(" "),n("li",[n("a",{attrs:{href:"https://juejin.im/post/5b2a186cf265da596d04a648#heading-1",target:"_blank",rel:"noopener noreferrer"}},[s._v("正则表达式 Unicode 转义"),n("OutboundLink")],1)]),s._v(" "),n("li",[n("a",{attrs:{href:"https://juejin.im/post/5b2a186cf265da596d04a648#heading-1",target:"_blank",rel:"noopener noreferrer"}},[s._v("非转义序列的模板字符串"),n("OutboundLink")],1)])]),s._v(" "),n("h3",{attrs:{id:"_1-异步迭代"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-异步迭代"}},[s._v("#")]),s._v(" 1.异步迭代")]),s._v(" "),n("p",[s._v("在"),n("code",[s._v("async/await")]),s._v("的某些时刻，你可能尝试在同步循环中调用异步函数。例如：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("async function process(array) {\n  for (let i of array) {\n    await doSomething(i);\n  }\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("p",[s._v("这段代码不会正常运行，下面这段同样也不会：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("async function process(array) {\n  array.forEach(async i => {\n    await doSomething(i);\n  });\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("p",[s._v("这段代码中，循环本身依旧保持同步，并在在内部异步函数之前全部调用完成。")]),s._v(" "),n("p",[s._v("ES2018引入异步迭代器（asynchronous iterators），这就像常规迭代器，除了"),n("code",[s._v("next()")]),s._v("方法返回一个Promise。因此"),n("code",[s._v("await")]),s._v("可以和"),n("code",[s._v("for...of")]),s._v("循环一起使用，以串行的方式运行异步操作。例如：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("async function process(array) {\n  for await (let i of array) {\n    doSomething(i);\n  }\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("h3",{attrs:{id:"_2-promise-finally"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-promise-finally"}},[s._v("#")]),s._v(" 2.Promise.finally()")]),s._v(" "),n("p",[s._v("一个Promise调用链要么成功到达最后一个"),n("code",[s._v(".then()")]),s._v("，要么失败触发"),n("code",[s._v(".catch()")]),s._v("。在某些情况下，你想要在无论Promise运行成功还是失败，运行相同的代码，例如清除，删除对话，关闭数据库连接等。")]),s._v(" "),n("p",[n("code",[s._v(".finally()")]),s._v("允许你指定最终的逻辑：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("function doSomething() {\n  doSomething1()\n  .then(doSomething2)\n  .then(doSomething3)\n  .catch(err => {\n    console.log(err);\n  })\n  .finally(() => {\n    // finish here!\n  });\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br")])]),n("h3",{attrs:{id:"_3-rest-spread-属性"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-rest-spread-属性"}},[s._v("#")]),s._v(" 3.Rest/Spread 属性")]),s._v(" "),n("p",[s._v("ES2015引入了"),n("a",{attrs:{href:"https://link.juejin.im/?target=https%3A%2F%2Fdeveloper.mozilla.org%2Fzh-CN%2Fdocs%2FWeb%2FJavaScript%2FReference%2FFunctions%2FRest_parameters",target:"_blank",rel:"noopener noreferrer"}},[s._v("Rest参数"),n("OutboundLink")],1),s._v("和"),n("a",{attrs:{href:"https://link.juejin.im/?target=https%3A%2F%2Fdeveloper.mozilla.org%2Fzh-CN%2Fdocs%2FWeb%2FJavaScript%2FReference%2FOperators%2FSpread_syntax",target:"_blank",rel:"noopener noreferrer"}},[s._v("扩展运算符"),n("OutboundLink")],1),s._v("。三个点（...）仅用于数组。Rest参数语法允许我们将一个不定数量的参数表示为一个数组。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("restParam(1, 2, 3, 4, 5);\n\nfunction restParam(p1, p2, ...p3) {\n  // p1 = 1\n  // p2 = 2\n  // p3 = [3, 4, 5]\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br")])]),n("p",[s._v("展开操作符以相反的方式工作，将数组转换成可传递给函数的单独参数。例如"),n("code",[s._v("Math.max()")]),s._v("返回给定数字中的最大值：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const values = [99, 100, -1, 48, 16];\nconsole.log( Math.max(...values) ); // 100\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("p",[s._v("ES2018为对象解构提供了和数组一样的Rest参数（）和展开操作符，一个简单的例子：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const myObject = {\n  a: 1,\n  b: 2,\n  c: 3\n};\n\nconst { a, ...x } = myObject;\n// a = 1\n// x = { b: 2, c: 3 }\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br")])]),n("p",[s._v("或者你可以使用它给函数传递参数：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("restParam({\n  a: 1,\n  b: 2,\n  c: 3\n});\n\nfunction restParam({ a, ...x }) {\n  // a = 1\n  // x = { b: 2, c: 3 }\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br")])]),n("p",[s._v("跟数组一样，Rest参数只能在声明的结尾处使用。此外，它只适用于每个对象的顶层，如果对象中嵌套对象则无法适用。")]),s._v(" "),n("p",[s._v("扩展运算符可以在其他对象内使用，例如：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const obj1 = { a: 1, b: 2, c: 3 };\nconst obj2 = { ...obj1, z: 26 };\n// obj2 is { a: 1, b: 2, c: 3, z: 26 }\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br")])]),n("p",[s._v("可以使用扩展运算符拷贝一个对象，像是这样"),n("code",[s._v("obj2 = {...obj1}")]),s._v("，但是 "),n("strong",[s._v("这只是一个对象的浅拷贝")]),s._v("。另外，如果一个对象A的属性是对象B，那么在克隆后的对象cloneB中，该属性指向对象B。")]),s._v(" "),n("h3",{attrs:{id:"_4-正则表达式命名捕获组"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-正则表达式命名捕获组"}},[s._v("#")]),s._v(" 4.正则表达式命名捕获组")]),s._v(" "),n("p",[s._v("JavaScript正则表达式可以返回一个匹配的对象——一个包含匹配字符串的类数组，例如：以"),n("code",[s._v("YYYY-MM-DD")]),s._v("的格式解析日期：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const\n  reDate = /([0-9]{4})-([0-9]{2})-([0-9]{2})/,\n  match  = reDate.exec('2018-04-30'),\n  year   = match[1], // 2018\n  month  = match[2], // 04\n  day    = match[3]; // 30\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("p",[s._v("这样的代码很难读懂，并且改变正则表达式的结构有可能改变匹配对象的索引。")]),s._v(" "),n("p",[s._v("ES2018允许命名捕获组使用符号"),n("code",[s._v("?<name>")]),s._v("，在打开捕获括号"),n("code",[s._v("(")]),s._v("后立即命名，示例如下：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const\n  reDate = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/,\n  match  = reDate.exec('2018-04-30'),\n  year   = match.groups.year,  // 2018\n  month  = match.groups.month, // 04\n  day    = match.groups.day;   // 30\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("p",[s._v("任何匹配失败的命名组都将返回"),n("code",[s._v("undefined")]),s._v("。")]),s._v(" "),n("p",[s._v("命名捕获也可以使用在"),n("code",[s._v("replace()")]),s._v("方法中。例如将日期转换为美国的 MM-DD-YYYY 格式：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const\n  reDate = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/,\n  d      = '2018-04-30',\n  usDate = d.replace(reDate, '$<month>-$<day>-$<year>');\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br")])]),n("h3",{attrs:{id:"_5-正则表达式反向断言"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-正则表达式反向断言"}},[s._v("#")]),s._v(" 5.正则表达式反向断言")]),s._v(" "),n("p",[s._v("目前JavaScript在正则表达式中支持先行断言（lookahead）。这意味着匹配会发生，但不会有任何捕获，并且断言没有包含在整个匹配字段中。例如从价格中捕获货币符号：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const\n  reLookahead = /\\D(?=\\d+)/,\n  match       = reLookahead.exec('$123.89');\n\nconsole.log( match[0] ); // $\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("p",[s._v("ES2018引入以相同方式工作但是匹配前面的反向断言（lookbehind），这样我就可以忽略货币符号，单纯的捕获价格的数字：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const\n  reLookbehind = /(?<=\\D)\\d+/,\n  match        = reLookbehind.exec('$123.89');\n\nconsole.log( match[0] ); // 123.89\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("p",[s._v("以上是 "),n("strong",[s._v("肯定反向断言")]),s._v("，非数字"),n("code",[s._v("\\D")]),s._v("必须存在。同样的，还存在 "),n("strong",[s._v("否定反向断言")]),s._v("，表示一个值必须不存在，例如：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const\n  reLookbehindNeg = /(?<!\\D)\\d+/,\n  match           = reLookbehind.exec('$123.89');\n\nconsole.log( match[0] ); // null\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("h3",{attrs:{id:"_6-正则表达式dotall模式"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_6-正则表达式dotall模式"}},[s._v("#")]),s._v(" 6.正则表达式dotAll模式")]),s._v(" "),n("p",[s._v("正则表达式中点"),n("code",[s._v(".")]),s._v("匹配除回车外的任何单字符，标记"),n("code",[s._v("s")]),s._v("改变这种行为，允许行终止符的出现，例如：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("/hello.world/.test('hello\\nworld');  // false\n/hello.world/s.test('hello\\nworld'); // true\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("h3",{attrs:{id:"_7-正则表达式-unicode-转义"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_7-正则表达式-unicode-转义"}},[s._v("#")]),s._v(" 7.正则表达式 Unicode 转义")]),s._v(" "),n("p",[s._v("到目前为止，在正则表达式中本地访问 Unicode 字符属性是不被允许的。ES2018添加了 Unicode 属性转义——形式为"),n("code",[s._v("\\p{...}")]),s._v("和"),n("code",[s._v("\\P{...}")]),s._v("，在正则表达式中使用标记 "),n("code",[s._v("u")]),s._v(" (unicode) 设置，在"),n("code",[s._v("\\p")]),s._v("块儿内，可以以键值对的方式设置需要匹配的属性而非具体内容。例如：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const reGreekSymbol = /\\p{Script=Greek}/u;\nreGreekSymbol.test('π'); // true\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br")])]),n("p",[s._v("此特性可以避免使用特定 Unicode 区间来进行内容类型判断，提升可读性和可维护性。")]),s._v(" "),n("h3",{attrs:{id:"_8-非转义序列的模板字符串"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_8-非转义序列的模板字符串"}},[s._v("#")]),s._v(" 8.非转义序列的模板字符串")]),s._v(" "),n("p",[s._v("之前，"),n("code",[s._v("\\u")]),s._v("开始一个 unicode 转义，"),n("code",[s._v("\\x")]),s._v("开始一个十六进制转义，"),n("code",[s._v("\\")]),s._v("后跟一个数字开始一个八进制转义。这使得创建特定的字符串变得不可能，例如Windows文件路径 "),n("code",[s._v("C:\\uuu\\xxx\\111")]),s._v("。更多细节参考"),n("a",{attrs:{href:"https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/template_strings",target:"_blank",rel:"noopener noreferrer"}},[s._v("模板字符串"),n("OutboundLink")],1),s._v("。")]),s._v(" "),n("h2",{attrs:{id:"es10新特性（2019）"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#es10新特性（2019）"}},[s._v("#")]),s._v(" ES10新特性（2019）")]),s._v(" "),n("ul",[n("li",[s._v("行分隔符（U + 2028）和段分隔符（U + 2029）符号现在允许在字符串文字中，与JSON匹配")]),s._v(" "),n("li",[s._v("更加友好的 JSON.stringify")]),s._v(" "),n("li",[s._v("新增了Array的"),n("code",[s._v("flat()")]),s._v("方法和"),n("code",[s._v("flatMap()")]),s._v("方法")]),s._v(" "),n("li",[s._v("新增了String的"),n("code",[s._v("trimStart()")]),s._v("方法和"),n("code",[s._v("trimEnd()")]),s._v("方法")]),s._v(" "),n("li",[n("code",[s._v("Object.fromEntries()")])]),s._v(" "),n("li",[n("code",[s._v("Symbol.prototype.description")])]),s._v(" "),n("li",[n("code",[s._v("String.prototype.matchAll")])]),s._v(" "),n("li",[n("code",[s._v("Function.prototype.toString()")]),s._v("现在返回精确字符，包括空格和注释")]),s._v(" "),n("li",[s._v("简化"),n("code",[s._v("try {} catch {}")]),s._v(",修改 "),n("code",[s._v("catch")]),s._v(" 绑定")]),s._v(" "),n("li",[s._v("新的基本数据类型"),n("code",[s._v("BigInt")])]),s._v(" "),n("li",[s._v("globalThis")]),s._v(" "),n("li",[s._v("import()")]),s._v(" "),n("li",[s._v("Legacy RegEx")]),s._v(" "),n("li",[s._v("私有的实例方法和访问器")])]),s._v(" "),n("h3",{attrs:{id:"_1-行分隔符（u-2028）和段分隔符（u-2029）符号现在允许在字符串文字中，与json匹配"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-行分隔符（u-2028）和段分隔符（u-2029）符号现在允许在字符串文字中，与json匹配"}},[s._v("#")]),s._v(" 1.行分隔符（U + 2028）和段分隔符（U + 2029）符号现在允许在字符串文字中，与JSON匹配")]),s._v(" "),n("p",[s._v("以前，这些符号在字符串文字中被视为行终止符，因此使用它们会导致SyntaxError异常。")]),s._v(" "),n("h3",{attrs:{id:"_2-更加友好的-json-stringify"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-更加友好的-json-stringify"}},[s._v("#")]),s._v(" 2.更加友好的 JSON.stringify")]),s._v(" "),n("p",[s._v("如果输入 Unicode 格式但是超出范围的字符，在原先JSON.stringify返回格式错误的Unicode字符串。现在实现了一个改变JSON.stringify的"),n("a",{attrs:{href:"https://github.com/tc39/proposal-well-formed-stringify",target:"_blank",rel:"noopener noreferrer"}},[s._v("第3阶段提案"),n("OutboundLink")],1),s._v("，因此它为其输出转义序列，使其成为有效Unicode（并以UTF-8表示）")]),s._v(" "),n("h3",{attrs:{id:"_3-新增了array的flat-方法和flatmap-方法"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-新增了array的flat-方法和flatmap-方法"}},[s._v("#")]),s._v(" 3.新增了Array的"),n("code",[s._v("flat()")]),s._v("方法和"),n("code",[s._v("flatMap()")]),s._v("方法")]),s._v(" "),n("p",[n("code",[s._v("flat()")]),s._v("和"),n("code",[s._v("flatMap()")]),s._v("本质上就是是归纳（reduce） 与 合并（concat）的操作。")]),s._v(" "),n("h4",{attrs:{id:"array-prototype-flat"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#array-prototype-flat"}},[s._v("#")]),s._v(" Array.prototype.flat()")]),s._v(" "),n("p",[n("code",[s._v("flat()")]),s._v(" 方法会按照一个可指定的深度递归遍历数组，并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。")]),s._v(" "),n("ul",[n("li",[n("code",[s._v("flat()")]),s._v("方法最基本的作用就是数组降维")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var arr1 = [1, 2, [3, 4]];\narr1.flat(); \n// [1, 2, 3, 4]\n\nvar arr2 = [1, 2, [3, 4, [5, 6]]];\narr2.flat();\n// [1, 2, 3, 4, [5, 6]]\n\nvar arr3 = [1, 2, [3, 4, [5, 6]]];\narr3.flat(2);\n// [1, 2, 3, 4, 5, 6]\n\n//使用 Infinity 作为深度，展开任意深度的嵌套数组\narr3.flat(Infinity); \n// [1, 2, 3, 4, 5, 6]\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br"),n("span",{staticClass:"line-number"},[s._v("16")]),n("br")])]),n("ul",[n("li",[s._v("其次，还可以利用"),n("code",[s._v("flat()")]),s._v("方法的特性来去除数组的空项")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var arr4 = [1, 2, , 4, 5];\narr4.flat();\n// [1, 2, 4, 5]\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br")])]),n("h4",{attrs:{id:"array-prototype-flatmap"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#array-prototype-flatmap"}},[s._v("#")]),s._v(" Array.prototype.flatMap()")]),s._v(" "),n("p",[n("code",[s._v("flatMap()")]),s._v(" 方法首先使用映射函数映射每个元素，然后将结果压缩成一个新数组。它与 map 和 深度值1的 flat 几乎相同，但 flatMap 通常在合并成一种方法的效率稍微高一些。 这里我们拿map方法与flatMap方法做一个比较。")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var arr1 = [1, 2, 3, 4];\n\narr1.map(x => [x * 2]); \n// [[2], [4], [6], [8]]\n\narr1.flatMap(x => [x * 2]);\n// [2, 4, 6, 8]\n\n// 只会将 flatMap 中的函数返回的数组 “压平” 一层\narr1.flatMap(x => [[x * 2]]);\n// [[2], [4], [6], [8]]\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br")])]),n("h3",{attrs:{id:"_4-新增了string的trimstart-方法和trimend-方法"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-新增了string的trimstart-方法和trimend-方法"}},[s._v("#")]),s._v(" 4.新增了String的"),n("code",[s._v("trimStart()")]),s._v("方法和"),n("code",[s._v("trimEnd()")]),s._v("方法")]),s._v(" "),n("p",[s._v("新增的这两个方法很好理解，分别去除字符串首尾空白字符，这里就不用例子说声明了。")]),s._v(" "),n("h3",{attrs:{id:"_5-object-fromentries"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-object-fromentries"}},[s._v("#")]),s._v(" 5."),n("code",[s._v("Object.fromEntries()")])]),s._v(" "),n("p",[n("code",[s._v("Object.entries()")]),s._v("方法的作用是返回一个给定对象自身可枚举属性的键值对数组，其排列与使用 for...in 循环遍历该对象时返回的顺序一致（区别在于 for-in 循环也枚举原型链中的属性）。")]),s._v(" "),n("p",[n("strong",[s._v("而Object.fromEntries() 则是 Object.entries() 的反转。")])]),s._v(" "),n("p",[n("code",[s._v("Object.fromEntries()")]),s._v(" 函数传入一个键值对的列表，并返回一个带有这些键值对的新对象。这个迭代参数应该是一个能够实现@iterator方法的的对象，返回一个迭代器对象。它生成一个具有两个元素的类似数组的对象，第一个元素是将用作属性键的值，第二个元素是与该属性键关联的值。")]),s._v(" "),n("ul",[n("li",[s._v("通过 Object.fromEntries， 可以将 Map 转化为 Object:")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const map = new Map([ ['foo', 'bar'], ['baz', 42] ]);\nconst obj = Object.fromEntries(map);\nconsole.log(obj); // { foo: \"bar\", baz: 42 }\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br")])]),n("ul",[n("li",[s._v("通过 Object.fromEntries， 可以将 Array 转化为 Object:")])]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ];\nconst obj = Object.fromEntries(arr);\nconsole.log(obj); // { 0: \"a\", 1: \"b\", 2: \"c\" }\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br")])]),n("h3",{attrs:{id:"_6-symbol-prototype-description"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_6-symbol-prototype-description"}},[s._v("#")]),s._v(" 6."),n("code",[s._v("Symbol.prototype.description")])]),s._v(" "),n("p",[s._v("通过工厂函数Symbol（）创建符号时，您可以选择通过参数提供字符串作为描述：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const sym = Symbol('The description');\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br")])]),n("p",[s._v("以前，访问描述的唯一方法是将符号转换为字符串：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("assert.equal(String(sym), 'Symbol(The description)');\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br")])]),n("p",[s._v("现在引入了getter Symbol.prototype.description以直接访问描述：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("assert.equal(sym.description, 'The description');\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br")])]),n("h3",{attrs:{id:"_7-string-prototype-matchall"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_7-string-prototype-matchall"}},[s._v("#")]),s._v(" 7."),n("code",[s._v("String.prototype.matchAll")])]),s._v(" "),n("p",[n("code",[s._v("matchAll()")]),s._v(" 方法返回一个包含所有匹配正则表达式及分组捕获结果的迭代器。 在 matchAll 出现之前，通过在循环中调用regexp.exec来获取所有匹配项信息（regexp需使用/g标志：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("const regexp = RegExp('foo*','g');\nconst str = 'table football, foosball';\n\nwhile ((matches = regexp.exec(str)) !== null) {\n  console.log(`Found ${matches[0]}. Next starts at ${regexp.lastIndex}.`);\n  // expected output: \"Found foo. Next starts at 9.\"\n  // expected output: \"Found foo. Next starts at 19.\"\n}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br")])]),n("p",[s._v("如果使用matchAll ，就可以不必使用while循环加exec方式（且正则表达式需使用／g标志）。使用matchAll 会得到一个迭代器的返回值，配合 for...of, array spread, or Array.from() 可以更方便实现功能：")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v('const regexp = RegExp(\'foo*\',\'g\'); \nconst str = \'table football, foosball\';\nlet matches = str.matchAll(regexp);\n\nfor (const match of matches) {\n  console.log(match);\n}\n// Array [ "foo" ]\n// Array [ "foo" ]\n\n// matches iterator is exhausted after the for..of iteration\n// Call matchAll again to create a new iterator\nmatches = str.matchAll(regexp);\n\nArray.from(matches, m => m[0]);\n// Array [ "foo", "foo" ]\n复制代码\n')])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br"),n("span",{staticClass:"line-number"},[s._v("14")]),n("br"),n("span",{staticClass:"line-number"},[s._v("15")]),n("br"),n("span",{staticClass:"line-number"},[s._v("16")]),n("br"),n("span",{staticClass:"line-number"},[s._v("17")]),n("br")])]),n("h4",{attrs:{id:"matchall可以更好的用于分组"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#matchall可以更好的用于分组"}},[s._v("#")]),s._v(" matchAll可以更好的用于分组")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("var regexp = /t(e)(st(\\d?))/g;\nvar str = 'test1test2';\n\nstr.match(regexp); \n// Array ['test1', 'test2']\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br")])]),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("let array = [...str.matchAll(regexp)];\n\narray[0];\n// ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', length: 4]\narray[1];\n// ['test2', 'e', 'st2', '2', index: 5, input: 'test1test2', length: 4]\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br")])]),n("h3",{attrs:{id:"_8-function-prototype-tostring-现在返回精确字符，包括空格和注释"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_8-function-prototype-tostring-现在返回精确字符，包括空格和注释"}},[s._v("#")]),s._v(" 8."),n("code",[s._v("Function.prototype.toString()")]),s._v("现在返回精确字符，包括空格和注释")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("function /* comment */ foo /* another comment */() {}\n\n// 之前不会打印注释部分\nconsole.log(foo.toString()); // function foo(){}\n\n// ES2019 会把注释一同打印\nconsole.log(foo.toString()); // function /* comment */ foo /* another comment */ (){}\n\n// 箭头函数\nconst bar /* comment */ = /* another comment */ () => {};\n\nconsole.log(bar.toString()); // () => {}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br"),n("span",{staticClass:"line-number"},[s._v("3")]),n("br"),n("span",{staticClass:"line-number"},[s._v("4")]),n("br"),n("span",{staticClass:"line-number"},[s._v("5")]),n("br"),n("span",{staticClass:"line-number"},[s._v("6")]),n("br"),n("span",{staticClass:"line-number"},[s._v("7")]),n("br"),n("span",{staticClass:"line-number"},[s._v("8")]),n("br"),n("span",{staticClass:"line-number"},[s._v("9")]),n("br"),n("span",{staticClass:"line-number"},[s._v("10")]),n("br"),n("span",{staticClass:"line-number"},[s._v("11")]),n("br"),n("span",{staticClass:"line-number"},[s._v("12")]),n("br"),n("span",{staticClass:"line-number"},[s._v("13")]),n("br")])]),n("h3",{attrs:{id:"_9-修改-catch-绑定"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_9-修改-catch-绑定"}},[s._v("#")]),s._v(" 9.修改 "),n("code",[s._v("catch")]),s._v(" 绑定")]),s._v(" "),n("p",[s._v("在 ES10 之前，我们必须通过语法为 catch 子句绑定异常变量，无论是否有必要。很多时候 catch 块是多余的。 ES10 提案使我们能够简单的把变量省略掉。")]),s._v(" "),n("p",[s._v("不算大的改动。")]),s._v(" "),n("p",[s._v("之前是")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("try {} catch(e) {}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br")])]),n("p",[s._v("现在是")]),s._v(" "),n("div",{staticClass:"language- line-numbers-mode"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[s._v("try {} catch {}\n复制代码\n")])]),s._v(" "),n("div",{staticClass:"line-numbers-wrapper"},[n("span",{staticClass:"line-number"},[s._v("1")]),n("br"),n("span",{staticClass:"line-number"},[s._v("2")]),n("br")])]),n("h3",{attrs:{id:"_10-新的基本数据类型bigint"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_10-新的基本数据类型bigint"}},[s._v("#")]),s._v(" 10.新的基本数据类型"),n("code",[s._v("BigInt")])]),s._v(" "),n("p",[s._v("现在的基本数据类型（值类型）不止5种（ES6之后是六种）了哦！加上BigInt一共有七种基本数据类型，分别是： String、Number、Boolean、Null、Undefined、Symbol、BigInt")])])}),[],!1,null,null,null);a.default=t.exports}}]);