当前位置:网站首页>深入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标准入门第三版
边栏推荐
- Preliminary exploration of flask: WSGI
- 浮点数如何与0进行比较?
- MySQL学习笔记——数据类型(数值类型)
- Optimization method of deep learning neural network
- AI做题水平已超过CS博士?
- Unity脚本常用API Day03
- Huawei cloud database DDS products are deeply enabled
- LeetCode 35. 搜索插入位置 —vector遍历(O(logn)和O(n)的写法---二分查找法)
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- 进制形式
猜你喜欢

Leecode learning notes - Joseph problem

大神详解开源 BUFF 增益攻略丨直播
Redis sentinel mode realizes one master, two slave and three Sentinels

TechSmith Camtasia studio 2022.0.2 screen recording software

华为云数据库DDS产品深度赋能

Memory management summary

What is the future of the booming intelligent Internet of things (aiot) in recent years?

Unity脚本常用API Day03

Redis 发布和订阅

力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
随机推荐
音视频技术开发周刊 | 252
每周招聘|高级DBA年薪49+,机会越多,成功越近!
%S format character
Unity动画Animation Day05
Redis哨兵模式实现一主二从三哨兵
Shell 编程基础
【读书会第十三期】 音频文件的封装格式和编码格式
MP3是如何诞生的?
Decimal, exponential
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
进制乱炖
How did the beyond concert 31 years ago get super clean and repaired?
近一亿美元失窃,Horizon跨链桥被攻击事件分析
Implementation of web chat room
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
【大连理工大学】考研初试复试资料分享
Flutter reports an error no mediaquery widget ancestor found
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
What are the concepts of union, intersection, difference and complement?