ECMA Script 6_解构赋值_模式匹配

解构赋值
从数组中提取值,按照对应位置,对变量赋值
只要等号右边的值不是对象或数组,就先将其转为对象。
由于 undefined 和 null 无法转为对象,所以对它们进行解构赋值,都会报错
let [a, b, c] = [1, 2, 3];
只要某种数据结构具有 Iterator 接口,都可以采用数组形式的解构赋值
应用:
const person = {
name: 'RyenToretto',
age: 22,
sex: '男'
}; // 解构赋值
const {name, age, sex} = person; // 缺点: 必须同名属性 console.log(name. age, sex); // 对比之前,明显好用了很多
console.log(person.name, person.age, person.sex);const arr = [1, 3, 5, 7, 9]; const [a, b, c, d, e, f=-1] = arr; // 是一组有序的赋值, 允许默认赋值 此时 f=-1,因为索引值为 undefined
const [aa, , cc] = arr; // aa=1, cc=5- 如果解构不成功,变量的值就等于undefined。
- 以下会报错
//右边的值 转为对象以后不具备 Iterator 接口
let [foo] = 1;
let [foo] = false;
let [foo] = NaN;
let [foo] = undefined;
let [foo] = null; let [foo] = {}; // {} 本身就不具备 Iterator 接口
- 解构赋值允许指定默认值
let [foo = true] = [];
foo // true let [x, y = 'b'] = ['a']; // x='a', y='b'
let [x, y = 'b'] = ['a', undefined]; // x='a', y='b'
注意,ES6 内部使用严格相等运算符(===),判断一个位置是否有值
所以,只有当一个数组成员严格等于undefined,默认值才会生效。
let [x = 1] = [undefined];
x // let [x = 1] = [null];
x // null
如果默认值是一个表达式,那么这个表达式是惰性求值的,即只有在用到的时候,才会求值
function f() {
console.log('aaa');
} let [x = f()] = [1]; // 由于 x 可以取 1 ,所以函数 f() 不会被执行- 默认值可以引用解构赋值的其他变量,但该变量必须已经声明
let [x = 1, y = x] = []; // x=1; y=1
let [x = 1, y = x] = [2]; // x=2; y=2
let [x = 1, y = x] = [1, 2]; // x=1; y=2
let [x = y, y = 1] = []; // Reference Error: y is not defined- 对象的解构赋值
let { foo, bar } = { foo: "aaa", bar: "bbb" };
foo // "aaa"
bar // "bbb"实际上的完整写法: let { foo: foo, bar: bar } = { foo: "aaa", bar: "bbb" };
- 也可以指定 默认值,生效的条件是,对象的属性值严格等于 undefined
var {x = 1, y = 2} = {x: 1}
console.log(x); //
console.log(y); //- 很有用的,sin(90*Math.PI/180);
let { log, sin, cos } = Math;
console.dir(Math);
console.dir(sin); // let {sin} = {sin:function(){}, cos:function(){}}- 由于数组本质是特殊的对象,因此可以对数组进行对象属性的解构
let arr = [1, 2, 3];
let {0 : first, [arr.length - 1] : last} = arr;
console.log(first); //
console.log(last); //- 字符串的解构赋值
此时,字符串被转换成了一个类似数组的对象
const [a, b, c, d, e] = 'hello';
console.log(a); // "h"
console.log(b); // "e"
console.log(c); // "l"
console.log(d); // "l"
console.log(e); // "o"- 类似数组的对象都有一个 length 属性,因此还可以对这个属性解构赋值 0
let {length : len} = 'hello';
console.log(len); //- 等号右边是数值和布尔值,则会先转为对象
let {toString: s} = 123;
console.log(s === Number.prototype.toString); // true let {toString: s} = true;
console.log(s === Boolean.prototype.toString); // true- 函数的 实参 与 形参 也可以进行 解构赋值
function add([x, y]){
return x + y;
} add([1, 2]); //- 函数 参数默认值 对象传参 参数是一组无序的值
function add({name = "someone", age = -1} = {name:"kjf", age:"22"}) {
return [name, age];
} console.log(add({name:"Jack", age:"31"})); // ["Jack", "31"]
console.log(add({name:"Rose", age:"21"})); // ["Rose", "21"]
console.log(add({})); // ["someone", -1]
console.log(add()); // ["kjf", "22"]- 函数 参数默认值 数组传参 参数是一组有序的值
function person([name="someone", age=-1] = ["kjf", 22]) {
return name + " : " +age;
} console.log(person(["Jack", 30])); // 'Jack : 30'
console.log(person(["Rose"])); // 'Rose : -1'
console.log(person([])); // 'someone : -1'
console.log(person()); // 'kjf : 22'
应用:
- 交换变量的值
let x = 1;
let y = 2; [x, y] = [y, x];- 快速提取 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]- for(let [attr1, attr2] of map){}
如此遍历 for(let [key, value] of map){}
任何部署了 Iterator 接口的对象,都可以用 for...of 循环遍历
// 获取键名
for (let [key] of map) {
// ...
} // 获取键值
for (let [,value] of map) {
// ...
}
ECMA Script 6_解构赋值_模式匹配的更多相关文章
- ECMA Script 6_数组的扩展_扩展运算符
1. 扩展运算符 内部调用的是数据结构的 Iterator 接口, 因此只要具有 Iterator 接口的对象,都可以使用扩展运算符 ... 如 map,,,, [...arr] 扩展运算符(spre ...
- 简单的axios请求返回数据解构赋值
本地 data.json 文件 { "name": "大熊", "age": 18, "fnc": [ 1, 2, 3 ...
- ECMA Script 6新特性之解构赋值
1.基本概念用法 1.1解构赋值:ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值. var a = 1; var b = 2; var c = 3; /*上述赋值语句用解构赋值为*/ v ...
- ES6_入门(4)_数组的解构赋值
//2017/7/14 //变量的解构赋值(解构:Destructuring) //(1)数组的解构赋值 let [a,b,c]=[1,2,3];//模式匹配,只要等号两边的模式相同,左边的变量就会被 ...
- ES6_入门(5)_解构赋值
学习参考:http://es6.ruanyifeng.com/#docs/destructuring //2017/7/20 /*对象的解构赋值*/ //对象的解构赋值,当变量名的对象的属性名相同时, ...
- ES6新特性:利用解构赋值 (destructuring assignment), 简化代码
本文的Demo的运行环境为nodeJS, 参考:让nodeJS支持ES6的词法----babel的安装和使用 : 解构赋值是一种表达式, 利用这种新语法, 可以直接从数组或者对象中快速提取值 赋值给不 ...
- ES6的变量解构赋值
前 言 ES6 解构赋值: ES6允许按照一定模式从数组和对象中提取值,然后对变量进行赋值,这被称为解构. 1.1 数组的结构赋值 1.1.1基本用法 JS中,为变量赋值直接指定.例如下面代码: ...
- ECMAScript 6 -- 数组的解构赋值
模式匹配:只要等号两边的模式相同,左边的变量就会被赋予对应的值. let [a, b, c] = [1, 2, 3]; 嵌套数组进行解构: let [foo, [[bar], baz]] = [1, ...
- ES6学习随笔--字符串模板、解构赋值、对象、循环、函数、Promise、Generrator
在线编译器:babel.github 在nongjs中使用 'use strict' let a = ; 运行node : node --harmony_destructuring xxx.js 代码 ...
随机推荐
- React 记录(3)
React文档:https://www.reactjscn.com/docs/hello-world.html 慢慢学习:对照教程文档,逐句猜解,截图 React官网:https://reactjs. ...
- Educational Codeforces Round 55 (Rated for Div. 2)
D. Maximum Diameter Graph 题意 给出每个点的最大度,构造直径尽可能长的树 思路 让度数大于$1$的点构成链,考虑是否能在链的两端加度为$1$的点 代码 #include &l ...
- 程序通过ReportViewer对象来调用reporting services并导出pdf文件
ReportViewer tempReportViewer = new ReportViewer(); tempReportViewer.ProcessingMode = ProcessingMode ...
- Java 线程安全LocalTime 和LocaldateTime 新的Date和Time类 -JDK8新时间类的简单使用
不可变类且线程安全 LocalDate .java.time.LocalTime 和LocaldateTime 新的Date和Time类 DateTimeFormatter ==https://ww ...
- smartgit
1.同步最新分支 2.smartgit ctrl+2 可以看到本地新增加的文件
- css基础二
1,文本 文本颜色: <style> body {color:red;} /*为body的所有字体设置字体颜色为红色*/ h1 {color:#00ff00;} /*为h1元素设置字体颜色 ...
- python开发遇到的坑(1)xpath解析ValueError: Unicode strings with encoding declaration are not supported
Traceback (most recent call last): File "/Users/*******.py", line 37, in <module> Bt ...
- php程序员招聘
岗位要求:-1年以上WEB端开发经验.-熟悉PHP语言的开发工作,熟练掌握LNMP开发,并具备良好的编程风格.-熟悉 http协议,掌握css js ajax 相关技术应用.-熟悉关系型数据,NOSQ ...
- ASP.NET Core学习之五 EntityFrameworkCore
目的:运用EntityFrameworkCore ,使用codefirst开发 一.创建web项目 创建一个不进行身份验证的 ASP.NET Core Web Application (.NET ...
- SQL Server2016安装
VS2017已经发布10多天了,这几天正好要重新做系统.所以想着把SQL Server和VS都做一次升级.VS2017只需要下载一个安装包就可以进行在线安装.但是SQL Server2016安装时会碰 ...