标签:targe nan tar 合并 foo 拷贝 col object turn
简介表示法(直接写入变量和函数,作为对象的属性和方法)
let x = "test" let obj={ x, //属性名为变量名,属性值为变量值 y(){console.log("hello")} } //以上相当于 let obj = { x:‘test‘, y:function(){ console.log("hello") } }
属性名表达式(表达式作为对象的属性名,把表达式放在方括号内。)
let aa = "hello"; let obj = { [aa]: "hello", ["a" + "b"]: "es6" } //以上相当于 let obj = { hello: "hello", "ab": "es6" }
对象方法的name属性(返回函数名)
let obj = { "hello": "hello", foo: function () { } } console.log(obj.foo.name)
Object.is()(比较两个值是否严格相等,相当于===,但是-0不等于0,NaN=NaN)
Object.is({}, {}) //false Object.is(-0, 0) //false Object.is(NaN, NaN) //true
Object.assign()(用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target))
let obj1 = {a:1}; let obj2 = {b:2}; let objx = {b:4}; let obj3 = Object.assign(obj1,obj2); //{ a: 1, b: 2 } let obj4 = Object.assign(obj1,obj2,obj3) //{ a: 1, b: 4 } 如果有相同的属性名,后者覆盖前者
注意:
const obj1 = {a: {b: 1}}; const obj2 = Object.assign({}, obj1); obj1.a.b = 2; obj2.a.b // 2
//把数组的索引当做key来生成对象 Object.assign([1, 2, 3], [4, 5]) //[4,5,3]
const source = { get foo(){ return 1 } } let a =Object.assign({},source) //{ foo: 1 }
Object.keys(),Object.values(),Object.entries()(都返回数组形式)
let obj = {a: 0,b: 1,c: 2}; let a = Object.keys(obj) //["a","b","c"] let b = Object.values(obj) //[0,1,2] let c = Object.entries(obj) //[["a":0],["b":1],["c":2]]
扩展运算符...
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 }; x // 1 y // 2 z // { a: 3, b: 4 } let obj = { a: { b: 1 } }; let { ...x } = obj; obj.a.b = 2; x.a.b // 2
let z = { a: 3, b: 4 }; let n = { ...z }; // { a: 3, b: 4 } //等同于 let n = Object.assign({},z) // { a: 3, b: 4 }
标签:targe nan tar 合并 foo 拷贝 col object turn
原文地址:http://www.cnblogs.com/sghy/p/7772113.html