对象简介语法(相当有用)
以json举例
let name = 'Strive';
let age = 18;
let json ={
name, //等同于name:name,
age, //等同于age:age
/* showA:function(){
return this.name;
} */ //方法简化成如下
showA(){
return this.name;
},
};
console.log(json);//{name: "Strive", age: 18, showA: ƒ}
console.log(json.showA());//Strive
其他示例
let x = 10,y =20;
function show({x,y}){
console.log(x, y);
}
show({x,y})//10 20 ,也是解构
new Vuex.Store({
state,
mutation,
types,
actions
});
new Vue({
router,
App,
vuex
})
//console.log(NaN == NaN);//false
//console.log(Number.isNaN(NaN));//true
let b = Object.is(NaN, NaN);//true
console.log(b);//true
//console.log(+0 == -0);//true
console.log(Object.is(+0, -0));//false
console.log(Object.is('aaa','aaa'));//true
Object.is():用来比较两个值是否相等
语法:Object.is('a','a');
比较两个东西相等还可以用: == 、===,但效果不一定一样
//console.log(NaN == NaN);//false
//console.log(Number.isNaN(NaN));//true
let b = Object.is(NaN, NaN);//true
console.log(b);//true
//console.log(+0 == -0);//true
console.log(Object.is(+0, -0));//false
console.log(Object.is('aaa','aaa'));//true
Object.assign()
用来合并对象,如果有重复的属性名,后面的对象里面的覆盖前面的。
格式:let 新的对象 = Object.assign(目标对象, source1, srouce2....)
用途:1.复制一个对象 2. 合并参数
let json={a:1},json2={b:2},json2={c:3};
let obj=Object.assign({},json,json2,json3);
console.log(obj);//{a:1,b:2,c:3}
let json1={a:2,b:2};
let obj2=Object.assign({},json,son1,json3);
console.log(obj);//{a:2,b:2,c:3}
数组也可以使用:
let arr = ['apple','banana','orange'];
let arr2 = Object.assign([], arr);
arr2.push('tomato');
console.log(arr2);//['apple','banana','orange','tomato']
应用,ajax传参后续新增或更改参数
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);//循环 a b c
}
let {keys, values, entries} = Object;//解构赋值省力写法
for(let key of keys(json)){
console.log(key);//循环 a b c
}
for(let value of values(json)){
console.log(value);//循环 1 2 3
}
for(let item of entries(json)){
console.log(typeof item);//循环 object
console.log(item);//["a", 1],["b", 2],["c", 3]
}
for(let [key, val] of entries(json)){
console.log(key, val);//循环a 1 b 2 c 3
}
对象: 计划在ES2018引入 ...
运用实例:
let json = {a:3, b:4};
let json2 = {...json};
console.log(json2);//{a: 3, b: 4}
delete json2.b;
console.log(json2);//{a: 3}
console.log(json);//{a: 3,b: 4}
let {x,y, ...z} ={x:1, y:2, a:3, b:4};
console.log(x,y, z);//1 2 {a: 3, b: 4}
网友评论