当前位置:网站首页>ES6, deep copy, shallow copy
ES6, deep copy, shallow copy
2022-07-28 22:34:00 【Crane sorrow_】
One 、 Concept
ES6:ESMAScript6, new javascript Grammar standards ,2015 Officially launched in , in order to js Faster and more convenient
Two 、 Variable declarations
var: Global scope
Function scope
No, if,switch,for,while,do,else Scope
Variable promotion
let:1. Local scope ( In pairs {} Enable )
2. You can't raise variables
3. Cannot declare repeatedly in a scope
const:1. let equally
2. Variable names are recommended to be capitalized
3. Declaration must be assigned
4. Value type cannot be modified
3、 ... and 、 deconstruction
Put the array , object , Deconstruct into separate variables
- Array
var [a,b,,c=20,...rest]=[1,2,3,4,5,6];- object
var {name,age,leg=20,...rest} = obj;Four 、let
- var You can declare variables
- let You can also declare local scope variables ( In pairs {} Enable )
- let local variable
- Cannot repeat declaration
- let You can't raise variables
var lis = document.querySelectorAll("ul li");
for(let i=0;i<lis.length;i++){
lis[i].onclick = function(){alert(i)}
}var h1 = document.querySelector("h1");
for(let i=9;i>=1;i--){
// Delay 10-i Seconds to perform
setTimeout(function(){
// Set the text value to i
h1.innerText = i;
},(10-i)*1000);
}5、 ... and 、const
characteristic :
- const It's a local variable
- Declaration must be assigned
- Cannot repeat declaration
- It is suggested to capitalize
- No variable promotion
- Value type cannot be modified
When to use const
- When declared variables are not allowed to be modified , When it's a constant
- When this pair probability will not be changed ( choice DOM node )
{
const PI = 3.1415926;
const PI;
console.log(PI);
}{
const LIST = ["I","Hate","You"];
LIST.push("LOve");
console.log(LIST);
}6、 ... and 、 Deep copy
Deep copy , Value type , And reference types are copied ( Don't affect each other )
Shallow copy , Value type copy , The reference type is reference
function deepCopy(obj){
// If typeof The value is object And not null
if(typeof obj === 'object'&&obj!=null){
var result;
// If it's an array
if(obj instanceof Array){
result = [];
// Traversal array , Execute Copy
for(var i=0;i<obj.length;i++){
// The value of the current subscript is the result of executing a deep copy
result[i]=deepCopy(obj[i])
}
}else{
// If it's an object
result = {};
for(var k in obj){
// Perform traversal of deep copy objects
result[k] = deepCopy(obj[k])
}
}
// The result returned
return result;
}else{
// Value type alive null Alive Function directly returns
return obj;
}
}7、 ... and 、 Shallow copy
Deep copy , Values and reference types are copied
var obj = {name:"mumu",age:18,friend:["A","B","C"]}
var obj1 = {};
for(var k in obj){
obj1[k] = obj[k];
}边栏推荐
- 微信小程序剪切图片的功能
- JS implementation generates a random key of specified length
- 容器化配置启动redis集群 单机6节点 3主3从
- Static route and default route experiment
- Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
- 软考网络工程师
- Ngrok intranet penetration
- Hcip experiment (15)
- SQL injection less34 (post wide byte injection + Boolean blind injection)
- mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
猜你喜欢
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints

Ruiji takeout - background login function development

MySQL installation and configuration (super detailed, simple and practical)

What is time complexity

Jmeter 安装第三方插件 Plugins Manager
![[Ruiji takeout project]day4 - dish management](/img/2a/2d9deb7a583aa37b38a67ef2c74ee7.png)
[Ruiji takeout project]day4 - dish management

Hcip experiment (14)

6K6w5LiA5qyh5pS75Ye75YiG5p6Q

XXX port is already in use

6K6w5LiA5qyh5pS75Ye75YiG5p6Q
随机推荐
mysql create语句能不能用来建立表结构并追加新的记录
Establishment of Ruiji takeout development environment
Closure, prototype and original link
Necessary for in-depth learning: split the data set, split the labels according to the split pictures, and check the interval of all marked labels
If you want to grow rapidly, you must first experience a major blow!
Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
静态成员static详解
Ngx+sql environment offline installation log (RPM installation)
【二叉树】二叉树中的伪回文路径
What is time complexity
log4j漏洞 elk平台 处理方法 (logstah5.5.1)
DOM programming + events
Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
Excel-VBA 快速上手(十三、日期的常见用法)
What does GPRS network mean
Awk blank line filtering
Att & CK preliminary understanding
XXX port is already in use
[CS231N]Lecture_ 2:Image Classification pipelin
Day3 classification management of Ruiji takeout project