博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
es6-变量的解构赋值
阅读量:4307 次
发布时间:2019-06-06

本文共 850 字,大约阅读时间需要 2 分钟。

从数组和对象中提取值,对变量进行赋值,这被称为解构

let [foo, [[bar], baz]] = [1, [[2], 3]];foo // 1bar // 2baz // 3let [ , , third] = ["foo", "bar", "baz"];third // "baz"let [x, , y] = [1, 2, 3];x // 1y // 3let [head, ...tail] = [1, 2, 3, 4];head // 1tail // [2, 3, 4]let [x, y, ...z] = ['a'];x // "a"y // undefined  // 如果解构不成功,变量的值就等于undefinedz // []
function* fibs() {  let a = 0;  let b = 1;  while (true) {    yield a;    [a, b] = [b, a + b];  }}let [first, second, third, fourth, fifth, sixth] = fibs();// 0,1,1,2,3,5
let [x, y, z] = new Set(['a', 'b', 'c']); // set,不重复的数组x // "a"

允许指定默认值当一个数组成员严格等于undefined,默认值才会生效

let [foo = true] = []; // foo // truelet [x, y = 'b'] = ['a']; // x='a', y='b'let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'let [x = 1] = [undefined]; // x // 1let [x = 1] = [null]; //x // null

 

转载于:https://www.cnblogs.com/avidya/p/10636664.html

你可能感兴趣的文章
代码小思考
查看>>
Unity中的销毁方法
查看>>
ceph删除pool提示(you must first set the mon_allow_pool_delete config option to true)解决办法...
查看>>
2016-7-15(1)使用gulp构建一个项目
查看>>
CSS 设计指南(第3版) 初读笔记
查看>>
markdown学习/mou
查看>>
CentOS 搭建 LAMP服务器
查看>>
看完此文再不懂区块链算我输,用Python从零开始创建区块链
查看>>
C/S框架-WebService架构用户凭证(令牌)解决方案
查看>>
UVA 11149.Power of Matrix-矩阵快速幂倍增
查看>>
ajax post 请求415\ 400 错误
查看>>
POJ 2696 计算表达式的值
查看>>
都江堰很美-佩服古人_Crmhf的一天
查看>>
Linux系统资源查询命令(cpu、io、mem)
查看>>
(转)PowerHA完全手册(一,二,三)
查看>>
hdu 4391 Paint The Wall 线段树 +优化 2012 Multi-University Training Contest 10 )
查看>>
socket与socketServer通信
查看>>
Android_Layout (一)
查看>>
《过早退出是一切失败的根源》读后感
查看>>
luogu P1774 最接近神的人_NOI导刊2010提高(02)
查看>>