当前位置:网站首页>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]边栏推荐
- insight! Baidu pushed redis ceiling notes, which was originally understood by the database
- 408复习策略(强化阶段)
- Ue5 gas learning notes 0.1 case Preview
- Tcp/ip detailed diagram
- Docker builds MySQL master-slave replication
- 微信公众号授权登录后报redirect_uri参数错误的问题
- 冒泡排序和相关视频
- 高德地图实现自定义小蓝点 自定义点标记 绘制多边形/圆形区域 根据地图的移动显示或者隐藏自定义点标记的相关实现
- UE5 GAS 学习笔记 1.7 任务Ability Tasks
- Ue5 gas learning notes 0.2 configuration plug-in
猜你喜欢

Mqtt over quic: the next generation Internet of things standard protocol injects new impetus into the message transmission scenario

DC-DC switching power supply

不理解模块化、组件化、插件化的区别怎么行?

直播|StarRocks 技术内幕 :低基数全局字典优化

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

Brief introduction to the principle of spectrometer II

Live broadcast starrocks technology insider: low base global dictionary optimization

Six countries in Europe and the United States launched an express line product to optimize the "end-to-end" performance service on the 5th

Introduction to advanced design system (ads) 2009 RF simulation

多线程与高并发—— 源码解析 AQS 原理
随机推荐
Introduction to advanced design system (ads) 2009 RF simulation
#夏日挑战赛#【FFH】JS自定义组件:DIY一个随点随用的键盘!(一)
Multithreading and high concurrency -- source code analysis AQS principle
TCP/IP详细图解
Experimental building - PHP Dafa
What is the employment prospect of software testing?
Detailed explanation of oscilloscope probe
连线:谁拥有未来的艺术?OpenAI允许Dall-E用户将作品商用化,目前而言
Digital torrent: resource reorganization and strategic conflict in enterprise transformation
Cloud container and cloud native
Novice record: some small knowledge of mechanical keyboard
顿悟!百度强推的Redis天花板笔记,原来数据库是这样理解的
Shenzhen offline registration starrocks on AWS: how to conduct rapid unified analysis of real-time data warehouses
UE5 GAS 学习笔记 1.8 游戏特效(GameplayCue)
408复习策略(强化阶段)
Leetcode79 method 1: deep search
MQTT over QUIC:下一代物联网标准协议为消息传输场景注入新动力
一文简述:SRv6基本原理
UE5 GAS 学习笔记 1.2游戏标签
Principle, classification and requirements of antenna