主要知识点:对象解构、数组解构、混合解构以及参数解构
![]()
《深刻理解ES6》笔记 目录node
对象解构简单的例子segmentfault
let node = { type: "Identifier", name: "foo" }; let { type, name } = node; console.log(type); // "Identifier" console.log(name); // "foo"
let node = { type: "Identifier", name: "foo" }, type = "Literal", name = 5; //使用解构来分配不一样的值 ({ type, name } = node); console.log(type); // "Identifier" console.log(name); // "foo"
在函数中使用解构赋值数组
let node = { type: "Identifier", name: "foo" }, type = "Literal", name = 5; function outputInfo(value) { console.log(value === node); // true } outputInfo({ type, name } = node); console.log(type); // "Identifier" console.log(name); // "foo"
当你使用解构赋值语句时,若是所指定的本地变量在对象中没有找到同名属性,那么该变量会被赋值为 undefined 。cookie
let node = { type: "Identifier", name: "foo" }; let { type, name, value } = node; console.log(type); // "Identifier" console.log(name); // "foo" console.log(value); // undefined
能够选择性地定义一个默认值,以便在指定属性不存在时使用该值:dom
let node = { type: "Identifier", name: "foo" }; let { type, name, value = true } = node; console.log(type); // "Identifier" console.log(name); // "foo" console.log(value); // true
let node = { type: "Identifier", name: "foo" }; //读取名为 type 的属性,并把它的值存储在变量 localType 上 let { type: localType, name: localName } = node; console.log(localType); // "Identifier" console.log(localName); // "foo"
也能够给变量别名添加默认值,依然是在本地变量名称后添加等号与默认值,例如:函数
let node = { type: "Identifier" }; //该语法实际上与传统对象字面量语法相反,传统语法将名称放在冒号左边、值放在冒号右边;而在本例中,则是名称在右边,须要进行值读取的位置则被放在了左边。 let { type: localType, name: localName = "bar" } = node; console.log(localType); // "Identifier" console.log(localName); // "bar"
使用相似于对象字面量的语法,能够深刻到嵌套的对象结构中去提取你想要的数据:spa
let node = { type: "Identifier", name: "foo", loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 } } }; //每当有一个冒号在解构模式中出现,就意味着冒号以前的标识符表明须要检查的位置,而冒号右侧则是赋值的目标。当冒号右侧存在花括号时,表示目标被嵌套在对象的更深一层中。 let { loc: { start }} = node; console.log(start.line); // 1 console.log(start.column); // 1
在对象的嵌套解构中一样能为本地变量使用不一样的名称:rest
let node = { type: "Identifier", name: "foo", loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 } } }; // 提取 node.loc.start let { loc: { start: localStart }} = node; console.log(localStart.line); // 1 console.log(localStart.column); // 1
let colors = [ "red", "green", "blue" ]; let [ firstColor, secondColor ] = colors; console.log(firstColor); // "red" console.log(secondColor); // "green"
let colors = [ "red", "green", "blue" ]; let [ , , thirdColor ] = colors; console.log(thirdColor); // "blue"
let colors = [ "red", "green", "blue" ], firstColor = "black", secondColor = "purple"; [ firstColor, secondColor ] = colors; console.log(firstColor); // "red" console.log(secondColor); // "green"
数组解构赋值一样容许在数组任意位置指定默认值。当指定位置的项不存在、或其值为undefined ,那么该默认值就会被使用:code
let colors = [ "red" ]; let [ firstColor, secondColor = "green" ] = colors; console.log(firstColor); // "red" console.log(secondColor); // "green"
在整个解构模式中插入另外一个数组模式,解构操做就会下行到嵌套的数组中,就像这样:对象
let colors = [ "red", [ "green", "lightgreen" ], "blue" ]; // 随后 let [ firstColor, [ secondColor ] ] = colors; console.log(firstColor); // "red" console.log(secondColor); // "green"
数组解构有个名为剩余项( rest items )的概念,它使用 ... 语法来将剩余的项目赋值给一个指定的变量:
三个点的解构赋值必须放在全部解构元素的最末尾,不然报错。
let colors = [ "red", "green", "blue" ]; let [ firstColor, ...restColors ] = colors; console.log(firstColor); // "red" console.log(restColors.length); // 2 console.log(restColors[0]); // "green" console.log(restColors[1]); // "blue"
也能够进行数组的克隆操做:
/ 在 ES5 中克隆数组 var colors = [ "red", "green", "blue" ]; var clonedColors = colors.concat(); console.log(clonedColors); //"[red,green,blue]" // 在 ES6 中克隆数组 let colors = [ "red", "green", "blue" ]; let [ ...clonedColors ] = colors; console.log(clonedColors); //"[red,green,blue]"
混合解构指的是对象和数组混合起来,执行解构操做
let node = { type: "Identifier", name: "foo", loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 } }, range: [0, 3] }; let { loc: { start }, range: [ startIndex ] } = node; console.log(start.line); // 1 console.log(start.column); // 1 console.log(startIndex); // 0
原函数写法:
// options 上的属性表示附加参数 function setCookie(name, value, options) { options = options || {}; let secure = options.secure, path = options.path, domain = options.domain, expires = options.expires; // 设置 cookie 的代码 } // 第三个参数映射到 options setCookie("type", "js", { secure: true, expires: 60000 });
问题:没法仅经过查看函数定义就判断出函数所指望的输入,必须阅读函数体的代码。
重写函数:
function setCookie(name, value, { secure, path, domain, expires }) { // 设置 cookie 的代码 } setCookie("type", "js", { secure: true, expires: 60000 });
参数解构有一个怪异点:默认状况下调用函数时未给参数解构传值会抛出错误:
// 出错! setCookie("type", "js");
能够这样写避免错误:
function setCookie(name, value, { secure, path, domain, expires } = {}) { // ... }
function setCookie(name, value, { secure = false, path = "/", domain = "example.com", expires = new Date(Date.now() + 360000000) } = {} ) { // ... }