一、数组的解构赋值

let [foo, [[bar], baz]] = [1, [[2], 3]];

  ①可多不可少,等号的右边是数组

let [x, y] = [1, 2, 3]; //可以
let [bar, foo] = [1]; //foo的值为undefined // 报错
let [foo] = 1;
let [foo] = false;
let [foo] = NaN;
let [foo] = undefined;
let [foo] = null;
let [foo] = {};
//(具备 Iterator 接口?)

 

  ②默认值

let [foo = true] = [];
foo // true

二、对象的解构赋值

  ①对象的属性没有次序,变量必须与属性同名,才能取到正确的值

let { foo, bar } = { foo: "aaa", bar: "bbb" };
foo // "aaa"
bar // "bbb"
//(全)
let { foo: foo, bar: bar } = { foo: "aaa", bar: "bbb" };

  ②对象的解构赋值的内部机制,是先找到同名属性,然后再赋给对应的变量

//foo是匹配的模式,baz才是变量
let { foo: baz } = { foo: "aaa", bar: "bbb" };
baz // "aaa"
foo // error: foo is not defined

  ③对数组进行对象解构

let arr = [1, 2, 3];
let {0 : first, [arr.length - 1] : last} = arr;
first //
last //

三、字符串的解构赋值
  ①

const [a, b, c, d, e] = 'hello';
a // "h"
b // "e"
e // "o"

  ②

let {length : len} = 'hello';
len //

四、数值和布尔值的解构赋值

  ①解构赋值时,如果等号右边是数值和布尔值,则会先转为对象。

//数值和布尔值的包装对象都有toString属性,因此变量s都能取到值
let {toString: s} = 123;
s === Number.prototype.toString // true let {toString: s} = true;
s === Boolean.prototype.toString // true

  

  ②undefined和null无法转为对象

五、函数参数的解构赋值

  ①传入参数时,数组参数被解构成变量x和y。

function add([x, y]){
  return x + y;
} add([1, 2]); //

  

  ②undefined会触发函数参数的默认值

[1, undefined, 3].map((x = 'yes') => x);
// [ 1, 'yes', 3 ]

六、圆括号问题(可能导致解构歧义)

  ①有好几条不能用的,我觉得还是尽量都不用吧。

  ②可以使用圆括号的情况只有一种:赋值语句的非模式部分,可以使用圆括号

七、用途

  ①交换变量的值

let x = 1;
let y = 2;
[x, y] = [y, x];

  ②从函数返回多个值

// 返回一个数组

function example() {
return [1, 2, 3];
}
let [a, b, c] = example(); // 返回一个对象 function example() {
return {
foo: 1,
bar: 2
};
}
let { foo, bar } = example();

  ③函数参数的定义

// 参数是一组有次序的值
function f([x, y, z]) { ... }
f([1, 2, 3]); // 参数是一组无次序的值
function f({x, y, z}) { ... }
f({z: 3, y: 2, x: 1});

  ④提取JSON数据

let jsonData = {
id: 42,
status: "OK",
data: [867, 5309]
}; let { id, status, data: number } = jsonData; console.log(id, status, number);
// 42, "OK", [867, 5309]

  ⑤函数参数的默认值(这样就避免了在函数体内部再写var foo = config.foo || 'default foo';这样的语句。)

jQuery.ajax = function (url, {
async = true,
beforeSend = function () {},
cache = true,
complete = function () {},
crossDomain = false,
global = true,
// ... more config
}) {
// ... do stuff
};

  ⑥遍历Map结构

var map = new Map();
map.set('first', 'hello');
map.set('second', 'world'); for (let [key, value] of map) {
console.log(key + " is " + value);
}
// first is hello
// second is world // 获取键名
for (let [key] of map) {
// ...
} // 获取键值(value前面还有逗号)
for (let [,value] of map) {
// ...
}

  ⑦输入模块的指定方法

const { SourceMapConsumer, SourceNode } = require("source-map");

最新文章

  1. freeCAD预选项编辑器
  2. 前台给后台传JSON字符串,后台解析并保存
  3. mxnet环境搭建随记
  4. 技术英文单词贴--C
  5. 详解Bootstrap网格系统
  6. C# HttpWebRequest提交数据方式浅析
  7. Lazarus中TScreen类使用介绍
  8. Bootstrap <第一篇>
  9. MVC4.0 如何设置默认静态首页index.shtml
  10. 百分比布局实现响应式布局在 IE6 中填坑思路
  11. Machine Learning Done Wrong
  12. OpenCV中IplImage和Mat间的相互转换
  13. Windows Phone使用sliverlight toolkit实现页面切换动画效果
  14. MySQL必知必会 学习笔记(一)
  15. 说声PHP的setter&getter(魔术)方法,你们辛苦了
  16. Basys3在线调试视频指南及代码
  17. Spring系列(三) Bean装配的高级技术
  18. 《Java编程思想》读书笔记-第一个Java程序
  19. Java使用Jedis操作Redis大全
  20. python3简单实现微信爬虫

热门文章

  1. JavaScript--DOM访问子结点childNodes
  2. SQL 理论知识总结
  3. 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)
  4. VMWare虚拟网络编辑
  5. 299 Bulls and Cows 猜数字游戏
  6. Java对象简单实用(计算器案例)
  7. Java常见面试问题: equals()与hashCode()的使用
  8. 一个JavaScript贷款计算器
  9. PHP语言开发Paypal支付demo的具体实现
  10. Android Programming 3D Graphics with OpenGL ES (Including Nehe's Port)