当前位置:网站首页>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
边栏推荐
猜你喜欢
Preliminary exploration of flask: WSGI
函数式接口,方法引用,Lambda实现的List集合排序小工具
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
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
MySQL学习笔记——数据类型(2)
Introduction to modern control theory + understanding
在芯片高度集成的今天,绝大多数都是CMOS器件
AI has surpassed Dr. CS in question making?
从0到1建设智能灰度数据体系:以vivo游戏中心为例
MySQL learning notes - data type (numeric type)
随机推荐
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
Introduction to modern control theory + understanding
压力、焦虑还是抑郁? 正确诊断再治疗
大神详解开源 BUFF 增益攻略丨直播
Preliminary exploration of flask: WSGI
Case sharing | integrated construction of data operation and maintenance in the financial industry
【学习笔记】拟阵
Halcon knowledge: NCC_ Model template matching
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
web聊天室实现
An article learns variables in go language
MP3是如何诞生的?
LeetCode 58. 最后一个单词的长度
Ffprobe common commands
Weibo and Huya advance into interest communities: different paths for peers
华为云数据库DDS产品深度赋能
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
js平铺数据查找叶子节点
[Dalian University of technology] information sharing of postgraduate entrance examination and re examination
Unity script introduction day01