当前位置:网站首页>Go deep into the details of deconstruction and assignment of several data types in JS
Go deep into the details of deconstruction and assignment of several data types in JS
2022-07-04 15:26:00 【InfoQ】
Deconstruct assignment
- deconstruction: Can be separated from the values in an array or object
- assignment: Assign values to variables based on the deconstructed data
var a = 1;
var b = 2;
let [a,b] = [1,2] // Equivalent to the above
An array of deconstruction
Nested deconstruction
let [a,[b],c] = [1,[1,2],3]
console.log(a,b,c) // 1 1 3
The extension operator deconstructs
...
let [a,...b] = [1,2,3,4]
console.log(a,b)
// a = 1 b = [2,3,4]
Incomplete deconstruction
let [, , a] = [1,2,3]
console.log(a) // 3
Unsuccessful deconstruction
let [a] = 1
console.log(a) // TypeError: 1 is not iterable
b
let [a,b] = [1]
console.log(a,b) // 1 undefined
About
interator
1 is not iterable
interator
interator
undefined
null
NaN
interator
interator
interator
Deconstruction assignment with default values
let [a,b=2] = [1]
console.log(a,b) // 1 2
let [c = 1] = [undefined]
console.log(c) // 1
About
undefined
The problem of
NaN
null
let [c = 1] = [NaN]
console.log(c) // NaN
let [c = 1] = [null]
console.log(c) // null
===
NaN
null
undefined
Object to deconstruct
Different from array deconstruction
let {name} = {name:" Pig ruffian bully "}
console.log(name) // Pig ruffian bully
Variable name does not match property name
:
let {name:difname} = {name:" Pig ruffian bully "}
console.log(difname) // Pig ruffian bully
name
Deconstruction assignment of multi-level nested objects
let people = {
name:" Pig ruffian bully ",
like:{
community:"juejin",
code:"js"
}
}
let {name,like:{code}} = people
console.log(name,code) // Pig ruffian bully js
code
like
like
code
Pattern problem
like
like is not defined
String deconstruction
let str = " Pig ruffian bully "
let {1:first} = str
console.log(first) // Ruffian
let str = " Pig ruffian bully "
let [a] = str
console.log(a) // The pig
let str = " Pig ruffian bully "
let {length} = str
console.log(length) // 4
Numeric and Boolean deconstruction
let { toFixed:a} = 123
console.log(a) // toFixed() { [native code] }
toFixed
Use prototype chain to judge
toFixed
console.log(a === Number.prototype.toFixed) // true
Deconstruction of function parameters
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)
Deconstructive ambiguity
===
reference
- ES6 Introduction to standards, Third Edition
边栏推荐
- Align left and right!
- On the implementation plan of MySQL explain
- How to rapidly deploy application software under SaaS
- Huawei cloud database DDS products are deeply enabled
- Temperature control system based on max31865
- 暑期复习,一定要避免踩这些坑!
- 十六进制
- I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
- Hexadecimal form
- Enter the width!
猜你喜欢
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
【大连理工大学】考研初试复试资料分享
Common API day03 of unity script
华为云数据库DDS产品深度赋能
LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
31年前的Beyond演唱会,是如何超清修复的?
03 storage system
Redis 發布和訂閱
每周招聘|高级DBA年薪49+,机会越多,成功越近!
MYSQL索引优化
随机推荐
Detailed explanation of MySQL composite index (multi column index) use and optimization cases
中国主要城市人均存款出炉,你达标了吗?
這幾年爆火的智能物聯網(AIoT),到底前景如何?
Shell programming basics
lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
Weekly recruitment | senior DBA annual salary 49+, the more opportunities, the closer success!
hexadecimal
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
力扣刷题01(反转链表+滑动窗口+LRU缓存机制)
Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
Redis 发布和订阅
I plan to teach myself some programming and want to work as a part-time programmer. I want to ask which programmer has a simple part-time platform list and doesn't investigate the degree of the receiv
Unity脚本常用API Day03
大神详解开源 BUFF 增益攻略丨直播
Redis 解决事务冲突之乐观锁和悲观锁
When synchronized encounters this thing, there is a big hole, pay attention!
CentOS 6.3 下 PHP编译安装JSON模块报错解决
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
They are all talking about Devops. Do you really understand it?
Summer Review, we must avoid stepping on these holes!