当前位置:网站首页>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
边栏推荐
- The per capita savings of major cities in China have been released. Have you reached the standard?
- 科普达人丨一文看懂阿里云的秘密武器“神龙架构”
- AI做题水平已超过CS博士?
- 输入宽度!
- [learning notes] matroid
- Shell 编程基础
- 【学习笔记】拟阵
- Unity预制件Prefab Day04
- Unity update process_ Principle of unity synergy
- Data Lake Governance: advantages, challenges and entry
猜你喜欢
Unity脚本常用API Day03
Summer Review, we must avoid stepping on these holes!
暑期复习,一定要避免踩这些坑!
[differential privacy and data adaptability] differential privacy code implementation series (XIV)
.Net 应用考虑x64生成
MySQL index optimization
Quelles sont les perspectives de l'Internet intelligent des objets (aiot) qui a explosé ces dernières années?
TechSmith Camtasia studio 2022.0.2 screen recording software
Ffprobe common commands
Live broadcast preview | PostgreSQL kernel Interpretation Series II: PostgreSQL architecture
随机推荐
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
左右对齐!
Unity预制件Prefab Day04
.Net 应用考虑x64生成
%S format character
Decimal, exponential
Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
案例分享|金融业数据运营运维一体化建设
Guitar Pro 8win10 latest guitar learning / score / creation
03 storage system
进制形式
%f格式符
PXE network
CentOS 6.3 下 PHP编译安装JSON模块报错解决
Deep learning network regularization
MySQL学习笔记——数据类型(数值类型)
[learning notes] matroid
基于MAX31865的温度控制系统
数据库函数的用法「建议收藏」
Unity script lifecycle day02