一:對象簡潔語法:
1、變量簡潔
<script>let name = 'Strive';let age = 18;let json ={name, //name:name,age //age:age};console.log(json);</script>
2、函數簡潔
let json ={name, //name:name,age, //age:age/* showA:function(){return this.name;} */showA(){//不要用箭頭函數return this.name;},showB(){return this.age;}};
3、有關解構
let x = 10;let y =20;function show({x,y}){console.log(x, y);}show({x,y})
二、對象新增
1、
Object.is():?? ?用來比較兩個值是否相等
?? ?Object.is('a','a');
補充:JavaScript NaN 屬性
NaN 屬性是代表非數字值的特殊值。該屬性用于指示某個值不是數字。可以把 Number 對象設置為該值,來指示其不是數字值。
提示:請使用 isNaN() 全局函數來判斷一個值是否是 NaN 值。
?Object.is(+0, -0); false
Object.is(NaN, NaN); false
2、Object.assign():?
用途:
?? ??? ?1. 復制一個對象
?? ??? ?2. 合并參數
let 新的對象 = Object.assign(目標對象, source1, srouce2....)
原則:后覆蓋前
例:
let json = {a:1};let json2 = {b:2, a:2};let json3 = {c:3};let obj = Object.assign({}, json, json2,json3);console.log(obj);
3、補充:
ES2017引入:
?? ?Object.keys()
?? ?Object.entries();
?? ?Object.values();
let {keys, values, entries} = Object;let json = {a:1,b:2,c:3};for(let key of keys(json)){console.log(key);}for(let value of values(json)){console.log(value);}for(let item of entries(json)){console.log(item);}for(let [key, val] of entries(json)){console.log(key, val);}
4、【ES2018】對象擴展運算符:...
let json = {a:3, b:4};let json2 = {...json};delete json2.b;console.log(json2);console.log(json);