1. Object.is()
比较两个值是否相等
Object.is('foo','foo') // true
Object.is({},{}) // false
2. Object.assign()
合并对象
let source1 = {a:1,b:1}
let source2 = {a:2,c:2}
let target = {b:0,c:0}
let target1 = Object.assign(target,source1,source2)
console.log(target); // { b: 1, c: 2, a: 2 }
console.log(target1);// { b: 1, c: 2, a: 2 }
3. Object.keys()
遍历对象,返回对象的key值
let arr = [1,2,3]
console.log(Object.keys(arr)); // 输出 [ '0', '1', '2' ]
let obj = { a: 1, b: 2, c: 3 }
console.log(Object.keys(obj)); // 输出 [ 'a', 'b', 'c' ]
4.Object.values()
遍历对象,返回对象的value值
let arr = [1,2,3]
console.log(Object.values(arr)); // 输出[1,2,3]
let obj = { a: 1, b: 2, c: 3 }
console.log(Object.values(obj)); // 输出 [1,2,3]
5.Object.entries()
遍历对象,返回key和value
let arr = [1,2,3]
console.log(Object.entries(arr)); // 输出[{0:'0',1:1},{0:'1',1:2},{0:'2',1:3}]
let obj = { a: 1, b: 2, c: 3 }
console.log(Object.entries(obj)); // 输出 [{0:'a',1:1},{0:'b',1:2},{0:'c',1:3}]
6. 给对象设置属性
const name = 'Allen';
let person = {name};
console.log(person); // {name:'Allen'}
思考:
for...in/of... 和Object.keys(), Object.values() 有什么区别?