当前位置:网站首页>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
边栏推荐
猜你喜欢
每周招聘|高级DBA年薪49+,机会越多,成功越近!
音视频技术开发周刊 | 252
How to handle exceptions in multithreading?
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
Summer Review, we must avoid stepping on these holes!
Unity动画Animation Day05
直播预告 | PostgreSQL 内核解读系列第二讲:PostgreSQL 体系结构
Halcon knowledge: NCC_ Model template matching
干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧
Preliminary exploration of flask: WSGI
随机推荐
Techsmith Camtasia Studio 2022.0.2屏幕录制软件
hexadecimal
Summer Review, we must avoid stepping on these holes!
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Unity脚本生命周期 Day02
Preliminary exploration of flask: WSGI
华为云数据库DDS产品深度赋能
函数式接口,方法引用,Lambda实现的List集合排序小工具
Redis publish and subscribe
What are the concepts of union, intersection, difference and complement?
【读书会第十三期】FFmpeg 查看媒体信息和处理音视频文件的常用方法
怎么判断外盘期货平台正规,资金安全?
Case sharing | integrated construction of data operation and maintenance in the financial industry
MySQL~MySQL给已有的数据表添加自增ID
在芯片高度集成的今天,绝大多数都是CMOS器件
Intelligent customer service track: Netease Qiyu and Weier technology play different ways
Huawei cloud database DDS products are deeply enabled
Force button brush question 01 (reverse linked list + sliding window +lru cache mechanism)
Understand the context in go language in an article
Introduction to modern control theory + understanding