对象简洁语法
let name="suosuo";
let age=18;
let json={
name,//name:name
age,//age:age
// showA:function(){
// return this.name;
// }
showA(){
return this.name;
};//是上面一种的简写
};
console.log(json);//{name: "suosuo", age: 18, showA: ƒ}
console.log(json.showA());
复制代码
let x=1;
let y=10;
function show({x,y}){
console.log(x,y);
}
show({x:1,y:10});
//因为上面有赋值,所以可以简写为如下:
show({x,y});
复制代码
新增内容
Object.is(对比内容一,对比内容二)
只要二者长得一样,结果就一定是true,比如NaN
console.log(NaN==NaN);//原来是false
console.log(Number.isNaN(NaN));//true,这里限定了NaN的类型是数字才是true
console.log(Object.is(NaN,NaN));//true
console.log(+0==-0);//true
console.log(Object.is(+0,-0));//false
console.log(Object.is(+0,-0));//false
复制代码
Object.assign(新的对象比如{},合并对象一,合并对象二……)用来合并对象
let json ={a:1};
let json2 = {b:2};
let json3 = {c:3};
let obj = Object.assign({},json,json2,json3);
console.log(obj); //a: 1
// b: 2
// c: 3
// __proto__: Object
//如果在json2中加一个a:5,结果中的a将会变成5
复制代码
let arr = ['apple','banana','orange'];
let arr2 = Object.assign([],arr);
arr2.push('tomato');
console.log(arr2);//"apple", "banana", "orange", "tomato"]
复制代码
Object.keys() Object.values() Object.entries()
// let json4={
// d:1,
// e:2,
// f:3
// };
// for (let key of Object.keys(json4)){
// console.log(key);//d e f
// };
let {keys,values,entries} = Object;
let json4={
d:1,
e:2,
f:3
};
for(let key of Object.keys(json4)){
console.log(key);//d e f如下都把object省略了,意义一样
};
for (let value of values(json4)){
console.log(value);//1 2 3
};
for(let item of entries(json4)){
console.log(item);//每一个选项如d:1都被拆分成一个length为2的数组
}
for(let [key,val] of entries(json4)){
console.log(key,val);
};
复制代码
...也可以用在对象身上
let {x,y,...z}={x:1,y:2,a:3,b:4};
console.log(x,y,z);
let json1 = {a:3,b:4};
let json2 ={...json1};
// delete json2.b;
console.log(json2);//{a: 3, b: 4}加了上一行就只剩下a
复制代码