ES6参考---对象扩展
一、总结
一句话总结:
1、Object.is(v1, v2):判断2个数据是否完全相等,字符串方式比较
2、Object.assign(target, source1, source2..):将源对象的属性复制到目标对象上
3、对象直接操作 __proto__ 属性:let obj2 = {};obj2.__proto__ = obj1;
<script type="text/javascript"> console.log(Object.is('abc', 'abc'));//true console.log(NaN == NaN);//false console.log(Object.is(NaN, NaN));//true console.log(0 == -0);//true console.log(Object.is(0, -0));//false //Object.assign(target, source1, source2..) let obj = {name : 'kobe', age : 39, c: {d: 2}}; let obj1 = {}; Object.assign(obj1, obj); console.log(obj1, obj1.name); //直接操作 __proto__ 属性 let obj3 = {name : 'anverson', age : 41}; let obj4 = {}; obj4.__proto__ = obj3; console.log(obj4, obj4.name, obj4.age); </script>
1、Object.assign(target, source1, source2..):将源对象的属性复制到目标对象上?
将obj的属性给obj1:Object.assign(obj1, obj);
//Object.assign(target, source1, source2..) let obj = {name : 'kobe', age : 39, c: {d: 2}}; let obj1 = {}; Object.assign(obj1, obj); console.log(obj1, obj1.name);
2、对象直接操作 __proto__ 属性?
对象可以直接通过 __proto__ 属性 来操作原型
let obj3 = {name : 'anverson', age : 41}; let obj4 = {}; obj4.__proto__ = obj3; console.log(obj4, obj4.name, obj4.age);
二、对象扩展
博客对应课程的视频位置:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>04_对象扩展</title> 6 </head> 7 <body> 8 <!-- 9 1. Object.is(v1, v2) 10 * 判断2个数据是否完全相等,字符串方式比较 11 2. Object.assign(target, source1, source2..) 12 * 将源对象的属性复制到目标对象上 13 3. 直接操作 __proto__ 属性 14 let obj2 = {}; 15 obj2.__proto__ = obj1; 16 --> 17 18 <script type="text/javascript"> 19 20 console.log(Object.is('abc', 'abc'));//true 21 console.log(NaN == NaN);//false 22 console.log(Object.is(NaN, NaN));//true 23 24 console.log(0 == -0);//true 25 console.log(Object.is(0, -0));//false 26 27 //Object.assign(target, source1, source2..) 28 let obj = {name : 'kobe', age : 39, c: {d: 2}}; 29 let obj1 = {}; 30 Object.assign(obj1, obj); 31 console.log(obj1, obj1.name); 32 33 //直接操作 __proto__ 属性 34 let obj3 = {name : 'anverson', age : 41}; 35 let obj4 = {}; 36 obj4.__proto__ = obj3; 37 console.log(obj4, obj4.name, obj4.age); 38 39 </script> 40 </body> 41 42 </html>