【学习笔记】ES6 标准 - 对象简洁语法
2019-06-11 本文已影响0人
Spidd
/*----------对象简洁语法-----------*/
// JSON 注:尽量不要在JSON中使用箭头函数;
{
let name = '奶牛';
let age = 18;
let json = {
name,
age,
// showA:function(){
// return this.name
// },
showA(){
return this.name
}
}
console.log(json.showA());
}
// Object.is();用来比较两个值是否相等;
{
console.log(NaN == NaN); //false
console.log(Object.is(NaN, NaN)); //true
console.log(+0 == -0); //true
console.log(Object.is(+0, -0)); //false
console.log("aaa" == "aac"); //false
console.log(Object.is("aaa", "aac")); //false
}
// Object.assign();用来合并对象 实际用途:1.复制对象, 2.合并参数(默认参数);
// Object.assign({},json1,json2,json3)
{
let json4 = [1,2,3,4];
let json5 = Object.assign([],json4);
json5.push(5)
console.log(json4,json5)
let json = {a:1};
let json2 = {b:2,a:2};
let json3 = {c:3};
let obj = Object.assign({},json,json2,json3);
console.log(obj);
function ajax(options) {
let defaults = {
type:'get',
data:'默认值'
};
let json = Object.assign({}, defaults, options)
console.log(json);
}
let type = 'post';
let data = '用户1';
ajax({type, data});
}
// Object.keys(); 对象key数组 循环 for(let key of arr){
{
/*老写法*/
/*let json = {
a:1,
b:2,
c:3,
d(){
return 4
}
};
for(let key of Object.keys(json)){
console.log(key)
}*/
let {keys, assign, entries} = Object;
let json = {
a:1,
b:2,
c:3,
d(){
return 4
}
};
for(let [key, val] of entries(json)){
console.log(key, val)
}
}