对象简介语法以及对象新增:
//个人建议: 一定注意,不要用箭头函数//第一种写法
let name = 'Strive';
let age = 18;
let json ={
name, //name:name,
age, //age:age
/* showA:function(){
return this.name;
} */
showA(){
return this.name;
},
showB(){
return this.age;
}
};
console.log(json.showA(), json.showB());//Strive 18
//第二种写法
let x = 10;
let y =20;
function show({x,y}){
console.log(x, y);
}
show({x,y})
let {x,y, ...z} ={x:1, y:2, a:3, b:4};//对象解构
console.log(x,y, z);//1 2 {a: 3, b: 4}
对象扩展运算符
let json = {a:3, b:4};
let json2 = {...json};
delete json2.b;//删除对象的某一个属性
console.log(json2);//{a: 3}
console.log(json);//{a: 3, b: 4}
Object.is(): 用来比较两个值是否相等
Object.is(‘a’,‘a’);
比较两个东西相等
Object.is(NaN, NaN);//不相等
Object.is(+0, -0);//不相等
Object.assign(): 用来合并对象
let 新的对象 = Object.assign(目标对象, source1, srouce2…)
assign合并对象
let json = {a:1};
let json2 = {b:2, a:2};
let json3 = {c:3};
let obj = Object.assign({}, json, json2,json3);
console.log(obj);// {a: 2, b: 2, c: 3}
assign举例用法
function ajax(options){ //用户传
let defaults={
type:'get',
header:
data:{}
....
};
let json = Object.assign({}, defaults, options);
.....
}
用途:
1. 复制一个对象
2. 合并参数
ES2017引入:
Object.keys()
Object.entries();
Object.values();
let {keys, values, entries} = Object;//解构
let json = {
a:1,
b:2,
c:3
};
//若不解构用法如下
for(let key of Object.keys(json)){
console.log(key);
//解构后的用法
for(let key of keys(json)){
console.log(key);
}
对象身上: 计划在ES2018引入
…
let json = {a:3, b:4};
let json2 = {...json};