当前位置:网站首页>深入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标准入门第三版
边栏推荐
- Unity脚本生命周期 Day02
- 输入宽度!
- Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
- Redis哨兵模式实现一主二从三哨兵
- Luo Gu - some interesting questions 2
- LeetCode 1184. Distance between bus stops -- vector clockwise and counterclockwise
- selenium 浏览器(2)
- 03-存储系统
- [local differential privacy and random response code implementation] differential privacy code implementation series (13)
- Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
猜你喜欢
Quick introduction to automatic control principle + understanding
They are all talking about Devops. Do you really understand it?
numpy笔记
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
AI做题水平已超过CS博士?
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
go-zero微服务实战系列(九、极致优化秒杀性能)
Deep learning neural network case (handwritten digit recognition)
Leetcode 1200 minimum absolute difference [sort] The Path of leetcode for heroding
随机推荐
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
TechSmith Camtasia studio 2022.0.2 screen recording software
Hexadecimal form
.Net 应用考虑x64生成
C1 certification learning notes 3 -- Web Foundation
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
Unity脚本API—Transform 变换
Summer Review, we must avoid stepping on these holes!
小数,指数
在芯片高度集成的今天,绝大多数都是CMOS器件
flutter 报错 No MediaQuery widget ancestor found.
Redis 發布和訂閱
odoo数据库主控密码采用什么加密算法?
文本挖掘工具的介绍[通俗易懂]
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
The per capita savings of major cities in China have been released. Have you reached the standard?
LeetCode 58. Length of the last word
Leetcode 1200 minimum absolute difference [sort] the way of leetcode in heroding
怎么判断外盘期货平台正规,资金安全?
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现