ECMAScript 6 入门学习笔记(二)——变量的解构赋值
2024-08-30 04:35:26
一、数组的解构赋值
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");
最新文章
- freeCAD预选项编辑器
- 前台给后台传JSON字符串,后台解析并保存
- mxnet环境搭建随记
- 技术英文单词贴--C
- 详解Bootstrap网格系统
- C# HttpWebRequest提交数据方式浅析
- Lazarus中TScreen类使用介绍
- Bootstrap <;第一篇>;
- MVC4.0 如何设置默认静态首页index.shtml
- 百分比布局实现响应式布局在 IE6 中填坑思路
- Machine Learning Done Wrong
- OpenCV中IplImage和Mat间的相互转换
- Windows Phone使用sliverlight toolkit实现页面切换动画效果
- MySQL必知必会 学习笔记(一)
- 说声PHP的setter&;getter(魔术)方法,你们辛苦了
- Basys3在线调试视频指南及代码
- Spring系列(三) Bean装配的高级技术
- 《Java编程思想》读书笔记-第一个Java程序
- Java使用Jedis操作Redis大全
- python3简单实现微信爬虫
热门文章
- JavaScript--DOM访问子结点childNodes
- SQL 理论知识总结
- 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)
- VMWare虚拟网络编辑
- 299 Bulls and Cows 猜数字游戏
- Java对象简单实用(计算器案例)
- Java常见面试问题: equals()与hashCode()的使用
- 一个JavaScript贷款计算器
- PHP语言开发Paypal支付demo的具体实现
- Android Programming 3D Graphics with OpenGL ES (Including Nehe&#39;s Port)