当前位置:网站首页>深入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
b
let [a,b] = [1]
console.log(a,b) // 1 undefined
关于
interator
1 is not iterable
interator
interator
undefined
null
NaN
interator
interator
interator
带有默认值的解构赋值
let [a,b=2] = [1]
console.log(a,b) // 1 2
let [c = 1] = [undefined]
console.log(c) // 1
关于
undefined
的问题
NaN
null
let [c = 1] = [NaN]
console.log(c) // NaN
let [c = 1] = [null]
console.log(c) // null
===
NaN
null
undefined
对象解构
与数组解构的不同
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
code
like
like
code
模式问题
like
like 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
利用原型链判断
toFixed
console.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标准入门第三版
边栏推荐
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- Introduction of text mining tools [easy to understand]
- Halcon knowledge: NCC_ Model template matching
- selenium 浏览器(2)
- Redis哨兵模式实现一主二从三哨兵
- 怎么判断外盘期货平台正规,资金安全?
- 直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
- Preliminary exploration of flask: WSGI
- Redis 解决事务冲突之乐观锁和悲观锁
- . Net applications consider x64 generation
猜你喜欢
UFO: Microsoft scholars have proposed a unified transformer for visual language representation learning to achieve SOTA performance on multiple multimodal tasks
智能客服赛道:网易七鱼、微洱科技打法迥异
近一亿美元失窃,Horizon跨链桥被攻击事件分析
微博、虎牙挺进兴趣社区:同行不同路
numpy笔记
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
What is the future of the booming intelligent Internet of things (aiot) in recent years?
flutter 报错 No MediaQuery widget ancestor found.
开源人张亮的 17 年成长路线,热爱才能坚持
C1 certification learning notes 3 -- Web Foundation
随机推荐
AI做题水平已超过CS博士?
Redis 發布和訂閱
Optimization method of deep learning neural network
IO flow: node flow and processing flow are summarized in detail.
.Net 应用考虑x64生成
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
What is the future of the booming intelligent Internet of things (aiot) in recent years?
力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
Redis 发布和订阅
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Luo Gu - some interesting questions
每周招聘|高级DBA年薪49+,机会越多,成功越近!
They are all talking about Devops. Do you really understand it?
在芯片高度集成的今天,绝大多数都是CMOS器件
Ffmpeg Visual Studio development (IV): audio decoding
Redis 解决事务冲突之乐观锁和悲观锁
输入宽度!
MySQL learning notes - data type (numeric type)
Unity脚本常用API Day03