当前位置:网站首页>2022.7.26 构造函数,面试:new的作用、深拷贝和浅拷贝
2022.7.26 构造函数,面试:new的作用、深拷贝和浅拷贝
2022-07-28 17:05:00 【长寿的秘诀就是睡.】
一、构造函数的写法
1.固定参数(不建议使用)
function Car(name, color, suv) {
this.name = name;
this.color = color;
this.suv = suv;
}
var benchi = new Car("benchi", "red", true);
console.log(benchi);使用这个方法时需要注意:
明确知道对象属性
位置要严格对应
2. 对象类型参数
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);
即使传参顺序不同也不影响并且有维护方便,使用方便的特点,推荐使用。
二、new 的作用(面试)
1.如果不用 new 直接调用构造函数,会提示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);由此可以确定,new 的第一个作用是把对象返回了回来(类似于函数没有 return 会提示 undefined )。
2.如果在没有 new 的情况下打印 this ,发现 this 指向 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,
});
由此可见 new 的第二个作用,把构造函数的this指向了要返回的对象。
3.总结new作用:
- 创建了新空对象
- 将构造函数的作用域赋值给新对象(this指向新对象)
- 执行构造函数代码 (为这个新对象添加属性)
- 返回新对象
三、深拷贝与浅拷贝(面试)
1.浅拷贝
定义:对于引用类型而言,指两个引用类型指向同一个地址,改变一个,另一个也会随之改变。
var person1 = { age: 20 };
var person2 = person1;
person1.age = 25;
console.log("person1:", person1, "person2:", person2);
2.深拷贝
定义:对于引用类型而言,复制后引用类型指向一个新的内存地址,两个对象改变互不影响。
var p1 = { age: 18 };
//JSON.stringify序列化 JSON.parse反序列化
var p2 = JSON.parse(JSON.stringify(p1));
p1.age = 20;
console.log(p2);
注意:基础类型数据的赋值与深拷贝类似,但并不是深拷贝。
var a = 1;
var b = a;
a = 10;
console.log(a, b); // 10 1
注意:数组的concat、slice是一层对象的深拷贝,如果对象的子属性是引用类型的话,就是浅拷贝。
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]边栏推荐
- haproxy实现代理配置
- 欧美六国最快5日达 菜鸟推出快线产品 优化“端到端”履约服务
- Go语言系列之日志库zap
- Detailed explanation of oscilloscope parameters
- 直播|StarRocks 技术内幕 :低基数全局字典优化
- Record your interview experience in Xiamen for two years -- Conclusion
- Detailed explanation of network RJ45 interface
- LeetCode79题 方法一:深度搜索
- Mongodb create index
- GO exe生成图标版本信息
猜你喜欢

NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!

Detailed explanation of oscilloscope probe

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

冒泡排序和相关视频

Brief introduction to the principle of spectrometer I

@Autowired与@Resource区别

TCP/IP详细图解

Seven steps, in-depth interpretation of data meaning

欧美六国最快5日达 菜鸟推出快线产品 优化“端到端”履约服务

GIS数据漫谈(六)— 投影坐标系统
随机推荐
Software testing needs more and more talents, but fewer people are on the road of testing?
Ue5 gas learning notes 0.1 case Preview
Shenzhen offline registration starrocks on AWS: how to conduct rapid unified analysis of real-time data warehouses
Docker搭建Mysql主从复制
There is a special cryptology language called asn.1
Digital torrent: resource reorganization and strategic conflict in enterprise transformation
Is it difficult for novices to change careers through self-study software testing?
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
Examples of AC simulation and S-parameter simulation of ADS simulation
How does Xiaobai learn software testing with zero foundation?
网络RJ45接口详解
Wired: who owns the art of the future? Openai allows dall-e users to commercialize their works. At present
Ue5 gas learning notes 1.10 prediction
#夏日挑战赛#【FFH】JS自定义组件:DIY一个随点随用的键盘!(一)
Ue5 gas learning notes 1.5 gameplay effects game effects
leetcode 二叉树类
UE5 GAS 学习笔记 1.8 游戏特效(GameplayCue)
Ue5 gas learning notes 1.4 attribute set
syntax error: non-declaration statement outside function bodygo 和 syntax error: unexpected {, expect
@Autowired与@Resource区别