当前位置:网站首页>Object. Assign () & JS (= >) arrow function & foreach () function

Object. Assign () & JS (= >) arrow function & foreach () function

2022-07-04 10:58:00 ths512

Object.assign()  Method to copy the values of all enumerable properties from one or more source objects to the target object . It will return the target object .
Object.assign(target, ...sources)    【target: Target audience 】,【souce: Source object ( Can be more )】
 Take a chestnut :
const object1 = {
  a: 1,
  b: 2,
  c: 3
};

const object2 = Object.assign({c: 4, d: 5}, object1);

console.log(object2.c, object2.d);
console.log(object1)  // { a: 1, b: 2, c: 3 }
console.log(object2)  // { c: 3, d: 5, a: 1, b: 2 }

 Be careful :
1. If the properties in the target object have the same key , Then the attribute will be overridden by the attribute in the source object . The properties of the subsequent source object will similarly override the properties of the previous source object 
2.Object.assign  Method copies only the properties of the source object itself and can be enumerated to the target object . This method uses the [[Get]] And the target 
 Object's [[Set]], So it calls correlation  getter  and  setter. therefore , It assigns properties , Instead of just copying or defining new properties . Such as 
 The merge source contains getter, This may make it unsuitable to incorporate new attributes into the prototype . To define properties ( Including its enumerability ) Copied to the 
 Prototype , You should use Object.getOwnPropertyDescriptor() and Object.defineProperty() .

 Object.assign() Use - Simple books One 、Object.assign() Copy of object Two 、Object.assign() A deep copy of an object 3、 Merge of objects 4、 Merge objects with the same properties 5. Inherited attributes and enumerable attributes cannot be copied ...https://www.jianshu.com/p/f9ec860ecd81

js(=>) Arrow function Detailed explanation A complete set of cases - Hailun - Blog Garden ES6 A new function has been added to the standard :Arrow Function( Arrow function ). Why call Arrow Function? Because it is defined by an arrow : The arrow function above is equivalent to : Arrow functions are equivalent to anonymous functions , And simplify https://www.cnblogs.com/hailun/p/6279029.html

js Medium foreach usage _fanfan_h The blog of -CSDN Blog _foreach usage forEach() Method to perform the provided function once for each element of the array .var array = ['a', 'b', 'c'];array.forEach(function(element) { console.log(element);}); Output is :a;b;c;forEach() Method to perform the provided function once for each element of the array . Always returns undefined; var arr = [1,2,3...https://blog.csdn.net/fanfan_h/article/details/91406310

原网站

版权声明
本文为[ths512]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141413109439.html