当前位置:网站首页>1. Deep copy 2. Call apply bind 3. For of in differences
1. Deep copy 2. Call apply bind 3. For of in differences
2022-07-07 13:22:00 【Growing sunflower】
1、Json.parse(Json.Stringfy()) Unable to implement functions and undefined Deep copy , How to solve it ?
Let's write a deep copy
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) {
// Filter out the attributes on the prototype
if (target.hasOwnProperty(key)) {
dist[key] = deepClone(target[key])
}
}
return dist
} else {
return target
}
}
2、call apply bind Use of methods
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: ' Zhang San ',
describe: function () {
return ' full name :'+ this.name;
}
};
console.log(person.describe());// full name : Zhang San
var A = {
name: ' Zhang San ',
describe: function () {
return ' full name :'+ this.name;
}
};
var B = {
name: ' Li Si '
};
B.describe = A.describe;
console.log(B.describe());
// " full name : Li Si "
function f() {
return ' full name :'+ this.name;
}
var A = {
name: ' Zhang San ',
describe: f
};
var B = {
name: ' Li Si ',
describe: f
};
console.log(A.describe()); // " full name : Zhang San "
console.log(B.describe()); // " full name : Li Si "
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 and for in difference
1、 Circular array
Difference one :for in and for of Circular arrays are OK ,for in The output is the of the array index Subscript , and for of The output is the value of each item of the array .
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、 Loop objects
Difference two :for in You can traverse objects ,for of Can't traverse object , Can only traverse with iterator Interface , for example 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. Array objects
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
}
}
summary :for in Suitable for traversing objects ,for of Suitable for traversing arrays .for in Traversing is the index of the array , Object properties , And the attributes on the prototype chain .
边栏推荐
- 飞桨EasyDL实操范例:工业零件划痕自动识别
- [Presto profile series] timeline use
- Read PG in data warehouse in one article_ stat
- Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
- MongoDB内部的存储原理
- Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
- JS判断一个对象是否为空
- QQ的药,腾讯的票
- Sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- 一文读懂数仓中的pg_stat
猜你喜欢
Cinnamon 任务栏网速
DHCP 动态主机设置协议 分析
Per capita Swiss number series, Swiss number 4 generation JS reverse analysis
提升树莓派性能的方法
Storage principle inside mongodb
Awk of three swordsmen in text processing
Milkdown 控件图标
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
QQ的药,腾讯的票
[Presto profile series] timeline use
随机推荐
Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
PAcP learning note 3: pcap method description
Mongodb slice summary
Vscode编辑器ESP32头文件波浪线不跳转彻底解决
Clion mingw64 Chinese garbled code
Awk of three swordsmen in text processing
RealBasicVSR测试图片、视频
[etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
提升树莓派性能的方法
学习突围2 - 关于高效学习的方法
测试下摘要
滑轨步进电机调试(全国海洋航行器大赛)(STM32主控)
MySQL master-slave replication
Cmu15445 (fall 2019) project 2 - hash table details
LIS 最长上升子序列问题(动态规划、贪心+二分)
基于鲲鹏原生安全,打造安全可信的计算平台
How to reset Firefox browser
PACP学习笔记一:使用 PCAP 编程
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images