当前位置:网站首页>1、深拷贝 2、call apply bind 3、for of for in 区别
1、深拷贝 2、call apply bind 3、for of for in 区别
2022-07-07 11:27:00 【成长中的向日葵】
1、Json.parse(Json.Stringfy())无法实现函数和undefined的深拷贝,如何解决呢?
来手写个深拷贝吧
function deepClone(target) {
if (target instanceof Object) {
let dist
if (target instanceof Array) {
dist = []
} else if (target instanceof Function) {
dist = function () {
return target.call(this, ...arguments)
}
} else {
dist = {}
}
for (let key in target) {
// 过滤掉原型身上的属性
if (target.hasOwnProperty(key)) {
dist[key] = deepClone(target[key])
}
}
return dist
} else {
return target
}
}
2、call apply bind方法的使用
var obj1 = {
a: 2,
b: 3,
say: function (x, y) {
return this.a + x + y
},
}
var obj2 = {
a: 3,
b: 4,
}
var s1 = obj1.say.call(obj2, 5, 6)
var s2 = obj1.say.apply(obj2, [7, 8])
var s3 = obj1.say.bind(obj2)
console.log(s1)
console.log(s2)
console.log(s3(1, 2))
function f1(x, y) {
console.log(x + y, this)
}
f1(10, 20) //30 undefined
f1.call() //NaN undefined
f1.apply() //NaN undefined
f1.call(null, 2, 3) //5 null
f1.apply(null, [2, 3]) //5 null
var f2 = f1.bind(null)
f2(2, 3) //5 null
var obj = {
age: 15,
name: 'lalal',
}
f1.call(obj, 2, 3) //5 {age: 15, name: 'lalal'}
f1.apply(obj, [2, 3]) //{age: 15, name: 'lalal'}
var f2 = f1.bind(obj)
f2(2, 3) //5 {age: 15, name: 'lalal'}
var person = {
name: '张三',
describe: function () {
return '姓名:'+ this.name;
}
};
console.log(person.describe());//姓名:张三
var A = {
name: '张三',
describe: function () {
return '姓名:'+ this.name;
}
};
var B = {
name: '李四'
};
B.describe = A.describe;
console.log(B.describe());
// "姓名:李四"
function f() {
return '姓名:'+ this.name;
}
var A = {
name: '张三',
describe: f
};
var B = {
name: '李四',
describe: f
};
console.log(A.describe()); // "姓名:张三"
console.log(B.describe()); // "姓名:李四"
var obj1 = {
name: 'zhangjing',
aaa: function () {
console.log(this.name)
},
bbb: function () {
setTimeout(
function () {
this.aaa()
}.apply(obj1),
3000
)
},
}
obj1.bbb()//zhangjing
3、for of 和 for in 区别
1、循环数组
区别一:for in 和 for of 都可以循环数组,for in 输出的是数组的index下标,而for of 输出的是数组的每一项的值。
const arr = ["orange", "apple", "banana", "pear"];
for (let key of arr) {
console.log(key); // orange apple banana pear
}
for (let key in arr) {
console.log(key); //0 1 2 3
}
for (const [key, value] of arr.entries()) {
console.log(key, value); //0 'orange' 1 'apple' 2 'banana' 3 'pear'
}
2、循环对象
区别二 :for in可以遍历对象,for of不能遍历对象,只能遍历带有iterator接口的,例如Set,Map,String,Array
const obj = { 1: "orange", 2: "apple", 3: "banana", 4: "pear" };
for (let key of obj){
console.log(key);// Error in created hook: "TypeError: [object Object] is not iterable!"
}
for (let key in obj) {
console.log(key); // 1 2 3 4
console.log(obj[key]); // orange apple banana pear
}
3.数组对象
const fruits = [
{ 1: "orange" },
{ 2: "apple" },
{ 3: "banana" },
{ 4: "pear" },
];
for (let value of fruits) {
console.log(value); //{1: 'orange'} {2: 'apple'} {3: 'banana'} {4: 'pear'}
for (let key in value) {
console.log(key, value[key]);//1 orange 2 apple 3 banana 4 pear
}
}
总结:for in适合遍历对象,for of适合遍历数组。for in遍历的是数组的索引,对象的属性,以及原型链上的属性。
边栏推荐
- RealBasicVSR测试图片、视频
- PAcP learning note 1: programming with pcap
- LED light of single chip microcomputer learning notes
- JS中为什么基础数据类型可以调用方法
- Differences between MySQL storage engine MyISAM and InnoDB
- 简单好用的代码规范
- MongoDB 分片总结
- Signal strength (RSSI) knowledge sorting
- Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
- COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
猜你喜欢
数字ic设计——SPI
关于 appium 如何关闭 app (已解决)
PAcP learning note 1: programming with pcap
. Net ultimate productivity of efcore sub table sub database fully automated migration codefirst
Adopt a cow to sprint A shares: it plans to raise 1.85 billion yuan, and Xu Xiaobo holds nearly 40%
TPG x AIDU|AI领军人才招募计划进行中!
OSI 七层模型
关于 appium 启动 app 后闪退的问题 - (已解决)
单片机学习笔记之点亮led 灯
【Presto Profile系列】Timeline使用
随机推荐
【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
【无标题】
关于 appium 启动 app 后闪退的问题 - (已解决)
滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
Signal strength (RSSI) knowledge sorting
将数学公式在el-table里面展示出来
Go语言学习笔记-结构体(Struct)
MongoDB 遇见 spark(进行整合)
PAcP learning note 1: programming with pcap
JNA学习笔记一:概念
Pay close attention to the work of safety production and make every effort to ensure the safety of people's lives and property
如何让electorn打开的新窗口在window任务栏上面
Conversion from non partitioned table to partitioned table and precautions
简单好用的代码规范
Initialization script
高端了8年,雅迪如今怎么样?
Unity build error: the name "editorutility" does not exist in the current context
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
信号强度(RSSI)知识整理
Practical example of propeller easydl: automatic scratch recognition of industrial parts