当前位置:网站首页>2022.7.26 constructor, interview: the role of new, deep copy and shallow copy
2022.7.26 constructor, interview: the role of new, deep copy and shallow copy
2022-07-28 18:43:00 【The secret of longevity is sleep】
One 、 How to write a constructor
1. Fixed parameter ( Not recommended )
function Car(name, color, suv) {
this.name = name;
this.color = color;
this.suv = suv;
}
var benchi = new Car("benchi", "red", true);
console.log(benchi);When using this method, you need to pay attention to :
Know object properties clearly
The position should be strictly corresponding
2. Object type parameter
function Car(obj) {
this.name = obj.name;
this.size = obj.size;
this.color = obj.color;
this.suv = obj.suv;
}
var audi = new Car({ size: "big", name: "audi", suv: true, color: "red" });
console.log(audi);
Even if the transfer order is different, it will not affect and it is convenient to maintain , Easy to use features , Recommended .
Two 、new The role of ( interview )
1. If not new Call the constructor directly , Will prompt undefined
function Student(obj) {
this.name = obj.name;
this.score = obj.score;
this.grade = obj.grade;
}
var stu1 = Student({
name: 'Jack',
score: 88,
grade: 3,
});
console.log(stu1);From this we can determine ,new The first function of is to return the object ( Similar to function without return Will prompt undefined ).
2. If there is no new In case of printing this , Find out this Point to window .
function Student(obj) {
this.name = obj.name;
this.score = obj.score;
this.grade = obj.grade;
console.log(this);
}
var stu1 = Student({
name: 'Jack',
score: 88,
grade: 3,
});
thus it can be seen new The second function of , Put the this Points to the object to be returned .
3. summary new effect :
- Created a new empty object
- Assign the scope of the constructor to the new object (this Point to a new object )
- Execute constructor code ( Add properties for this new object )
- Return to new object
3、 ... and 、 Deep copy and shallow copy ( interview )
1. Shallow copy
Definition : For reference types , Refers to two reference types pointing to the same address , Change one , The other will change with it .
var person1 = { age: 20 };
var person2 = person1;
person1.age = 25;
console.log("person1:", person1, "person2:", person2);
2. Deep copy
Definition : For reference types , After copying, the reference type points to a new memory address , Two object changes do not affect each other .
var p1 = { age: 18 };
//JSON.stringify serialize JSON.parse Deserialization
var p2 = JSON.parse(JSON.stringify(p1));
p1.age = 20;
console.log(p2);
Be careful : The assignment of basic type data is similar to that of deep copy , But it's not a deep copy .
var a = 1;
var b = a;
a = 10;
console.log(a, b); // 10 1
Be careful : Array of concat、slice Is a deep copy of a layer of objects , If the sub attribute of an object is a reference type , Shallow copy .
var a = [1, 2, 3];
var b = [4, 5];
var ab = a.concat(b);
a = [2, 3];
console.log(ab); // [1,2,3,4,5]边栏推荐
- NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!
- How does Xiaobai learn software testing with zero foundation?
- Record your interview experience in Xiamen for two years -- Conclusion
- Ue5 gas learning notes 1.3 attribute
- Summer Challenge [FFH] JS custom component: DIY a keyboard that can be used at any time! (I)
- Golang并发模型之
- Mongodb create index
- MongoDB数据库复制表
- Leetcode79 method 1: deep search
- haproxy实现代理配置
猜你喜欢

腾讯汤道生:开源是产业互联网时代新的生产方式和协作模式

1.1、稀疏数组

连线:谁拥有未来的艺术?OpenAI允许Dall-E用户将作品商用化,目前而言

MySQL index usage and optimization

Tcp/ip detailed diagram

Docker搭建Mysql主从复制

Tencent Tang Daosheng: open source is a new mode of production and collaboration in the era of industrial Internet

Composition and principle of vector network analyzer (vector network)

Introduction to the principle of signal source

2022-07-27 第四小组 修身课 学习笔记(every day)
随机推荐
UE5 GAS 学习笔记 1.2游戏标签
Tcp/ip detailed diagram
苹果开发完整的苹果证书与描述文件创建流程
Log base zap of go language series
When golang encounters high concurrency seckill
Iptables configuration
What is the future of software testing?
Is it really realistic that people who have not been exposed to software testing can take up their posts after two months of training?
Go exe generates icon version information
Go concurrency one
Brief introduction to the principle of spectrometer I
UE5 GAS 学习笔记 1.8 游戏特效(GameplayCue)
深圳线下报名|StarRocks on AWS:如何对实时数仓进行极速统一分析
连线:谁拥有未来的艺术?OpenAI允许Dall-E用户将作品商用化,目前而言
Introduction to oscilloscope
UE5 GAS 学习笔记 1.3属性Attribute
Implementation of solid transfer function (based on transfer)
多线程与高并发—— 源码解析 AQS 原理
Msg.value of solidity
Digital torrent: resource reorganization and strategic conflict in enterprise transformation