当前位置:网站首页>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 .
边栏推荐
- Read PG in data warehouse in one article_ stat
- Some principles of mongodb optimization
- Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
- Vscade editor esp32 header file wavy line does not jump completely solved
- RecyclerView的数据刷新
- 10 张图打开 CPU 缓存一致性的大门
- Users, groups, and permissions
- [learning notes] zkw segment tree
- 单片机学习笔记之点亮led 灯
- Isprs2021/ remote sensing image cloud detection: a geographic information driven method and a new large-scale remote sensing cloud / snow detection data set
猜你喜欢
Vscade editor esp32 header file wavy line does not jump completely solved
Star Enterprise Purdue technology layoffs: Tencent Sequoia was a shareholder who raised more than 1billion
【黑马早报】华为辟谣“军师”陈春花;恒驰5预售价17.9万元;周杰伦新专辑MV 3小时播放量破亿;法华寺回应万元月薪招人...
基于鲲鹏原生安全,打造安全可信的计算平台
“新红旗杯”桌面应用创意大赛2022
About how appium closes apps (resolved)
数字ic设计——SPI
我那“不好惹”的00后下属:不差钱,怼领导,抵制加班
leecode3. 无重复字符的最长子串
详细介绍六种开源协议(程序员须知)
随机推荐
Differences between MySQL storage engine MyISAM and InnoDB
一文读懂数仓中的pg_stat
LeetCode_二分搜索_中等_153.寻找旋转排序数组中的最小值
Introduce six open source protocols in detail (instructions for programmers)
记一次 .NET 某新能源系统 线程疯涨 分析
regular expression
COSCon'22 社区召集令来啦!Open the World,邀请所有社区一起拥抱开源,打开新世界~
数字ic设计——SPI
Day26 IP query items
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
MATLAB中polarscatter函数使用
Why can basic data types call methods in JS
单片机原理期末复习笔记
Initialization script
Analysis of DHCP dynamic host setting protocol
【学习笔记】AGC010
分屏bug 小记
How to reset Google browser? Google Chrome restore default settings?
Clion mingw64 Chinese garbled code
[learning notes] agc010