ES6新增对象的方法

1.Object.is(): 判断两个值是否相等
Object.is(“NaN”,“NaN”) ------->true
修正了原来ES5中关于NaN存在的问题

2.Object.assign(): 用来合并对象

	let json1 = {a:1};
	let json2 = {b:2};
	let json3 = {c:3};
	let obj = Object.assign( {},json1,json2,json3 );
	console.log(obj);    //obj = {a:1,b:2,c:3}

如果对象中有重复的元素的话,那么前边的属性值会被后边对象的属性值覆盖

	let json1 = {a:1};
	let json2 = {b:2};
	let json3 = {b:3};
	let obj = Object.assign( {},json1,json2,json3 );
	console.log(obj);    //obj = {a:1,b:3}

还可以利用Object.assign方法来复制对象

	let person = {
	    name: "wang",
	    age: 18,
	    sex: "female"
	}
	let newObj = Object.assign({},person);
	newObj.lastName = "Yuan";
	console.log(newObj);    //{name: "wang", age: 18, sex: "female", lastName: "Yuan"}
	console.log(person);   //{name: "wang", age: 18, sex: "female"}

3.Object.keys():返回对象的索引,返回值是一个数组
4.扩展运算符也可以应用在对象身上
例如:

let json = {
            a: 2,
            b: 3,
            c: 4,
            d: 5
        };
        let {a,b,...c} = json;
        console.log(a,b,c);   //2 3 {c: 4, d: 5}

还可以利用扩展运算符快速复制对象

let json2 = {...json};    //json2 =  {a: 2,b: 3, c: 4,d: 5}

你可能感兴趣的:(javascript)