当前位置:网站首页>深入JS中几种数据类型的解构赋值细节
深入JS中几种数据类型的解构赋值细节
2022-07-04 14:29:00 【InfoQ】
解构赋值
- 解构:可以在数组或对象中的值分离出来
- 赋值:将解构出来的数据对变量进行赋值
var a = 1;
var b = 2;
let [a,b] = [1,2] // 等价于上述
数组解构
嵌套解构
let [a,[b],c] = [1,[1,2],3]
console.log(a,b,c) // 1 1 3
扩展运算符解构
...let [a,...b] = [1,2,3,4]
console.log(a,b)
// a = 1 b = [2,3,4]
不完全解构
let [, , a] = [1,2,3]
console.log(a) // 3
不成功的解构
let [a] = 1
console.log(a) // TypeError: 1 is not iterable
blet [a,b] = [1]
console.log(a,b) // 1 undefined
关于
interator
1 is not iterableinteratorinteratorundefinednullNaNinteratorinteratorinterator带有默认值的解构赋值
let [a,b=2] = [1]
console.log(a,b) // 1 2
let [c = 1] = [undefined]
console.log(c) // 1
关于
undefined
的问题
NaNnulllet [c = 1] = [NaN]
console.log(c) // NaN
let [c = 1] = [null]
console.log(c) // null
===NaNnullundefined对象解构
与数组解构的不同
let {name} = {name:"猪痞恶霸"}
console.log(name) // 猪痞恶霸
变量名与属性名不一致
:let {name:difname} = {name:"猪痞恶霸"}
console.log(difname) // 猪痞恶霸
name多层嵌套对象的解构赋值
let people = {
name:"猪痞恶霸",
like:{
community:"juejin",
code:"js"
}
}
let {name,like:{code}} = people
console.log(name,code) // 猪痞恶霸 js
codelikelikecode模式问题
likelike is not defined字符串解构
let str = "猪痞恶霸"
let {1:first} = str
console.log(first) // 痞
let str = "猪痞恶霸"
let [a] = str
console.log(a) // 猪
let str = "猪痞恶霸"
let {length} = str
console.log(length) // 4
数值与布尔值解构
let { toFixed:a} = 123
console.log(a) // toFixed() { [native code] }
toFixed利用原型链判断
toFixedconsole.log(a === Number.prototype.toFixed) // true
函数参数解构
function add([x,y]) {
return x+y
}
add([1,2]) // 3
let arr = [[1,2],[2,3]]
arr.map((item) => item[0]+item[1])
let arr = [[1,2],[2,3]]
arr.map(([a,b]) => a+b)
解构的歧义
===参考文献
- ES6标准入门第三版
边栏推荐
- Temperature control system based on max31865
- LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
- 一篇文章学会GO语言中的变量
- 一篇文章搞懂Go语言中的Context
- Redis 發布和訂閱
- Redis sentinel mode realizes one master, two slave and three Sentinels
- . Net applications consider x64 generation
- [local differential privacy and random response code implementation] differential privacy code implementation series (13)
- Summer Review, we must avoid stepping on these holes!
- Redis哨兵模式实现一主二从三哨兵
猜你喜欢

Halcon knowledge: NCC_ Model template matching
Redis sentinel mode realizes one master, two slave and three Sentinels

Implementation of macro instruction of first-order RC low-pass filter in signal processing (easy touch screen)

Unity动画Animation Day05

Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist

Unity脚本常用API Day03

在芯片高度集成的今天,绝大多数都是CMOS器件

产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现

Flutter reports an error no mediaquery widget ancestor found

暑期复习,一定要避免踩这些坑!
随机推荐
一篇文章学会GO语言中的变量
Temperature control system based on max31865
深度学习 神经网络案例(手写数字识别)
Introduction of text mining tools [easy to understand]
谈SaaS下如何迅速部署应用软件
. Net applications consider x64 generation
Summer Review, we must avoid stepping on these holes!
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
深度学习 网络正则化
Partial modification - progressive development
CentOS 6.3 下 PHP编译安装JSON模块报错解决
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
微博、虎牙挺进兴趣社区:同行不同路
进制形式
2022 financial products that can be invested
开源人张亮的 17 年成长路线,热爱才能坚持
深度学习 神经网络的优化方法
Enter the width!