let testObj = {};
//禁用可配置
Object.defineProperty(testObj,'name',{
configurable:false,
value:'123'
});
//testObj.name = '123';
console.log(testObj.name);
delete testObj.name;
console.log(testObj.name);
testObj.name = 333;
console.log(JSON.stringify(testObj))
var newObj = Object.assign(testObj,{});
console.log(newObj.name);
testObj = {name:333};
console.log(testObj.name);
console.log('-------------------Enumerable------------------')
let testObj2 = {};
Object.defineProperty(testObj2,'name',{
enumerable:false,
value:123
});
testObj2.age = 1;
console.log(testObj2);
for(let k in testObj2){
console.log(k);
}
console.log('-----------------------------------')
let testObj3 = {};
Object.defineProperties(testObj3,{
p1:{
value:'p1'
},
p2_:{
value:1
},
p2:{
get(){
return this.p2_;
},
set(v){
this.p2_ = v;
}
}
})
console.log('--------------------------------------');
//无法显示方法的打印
console.log(Object.getOwnPropertyDescriptor(testObj3,'p1').writable)
console.log(Object.getOwnPropertyDescriptors(testObj3));
console.log(testObj3.hasOwnProperty('p1'));
console.log(testObj3.propertyIsEnumerable('p1'));
console.log('--------------------------------------------')
let dest, src, result; /** * 简单复制 */
dest = {}; src = { id: 'src' };
result = Object.assign(dest, src);
// Object.assign 修改目标对象
// 也会返回修改后的目标对象
console.log(dest === result);
// true console.log(dest !== src); console.log(result); console.log(dest);
// true
// { id: src } // { id: src }
/** * 多个源对象 */
dest = {};
result = Object.assign(dest, { a: 'foo' }, { b: 'bar' });
console.log(result); // { a: foo, b: bar }
/** * 获取函数与设置函数 */
dest = {
set a(val) {
console.log(`Invoked dest setter with param ${val}`);
}
};
src = {
get a() {
console.log('Invoked src getter'); return 'foo';
}
};
Object.assign(dest, src);
// 调用 src 的获取方法
// 调用 dest 的设置方法并传入参数"foo"
// 因为这里的设置函数不执行赋值操作
// 所以实际上并没有把值转移过来
console.log(dest); // { set a(val) {...} }
console.log('-----------Object.is()---------')
function recursivelyCheckEqual(x, ...rest) {
return (Object.is(x, rest[0]) && (rest.length < 2 || recursivelyCheckEqual(...rest)))
}
console.log(recursivelyCheckEqual(NaN,NaN,NaN));
console.log(Object.is(NaN,NaN));
console.log(Object.is({},{}));
console.log(+0 === -0);
console.log(Object.is(+0,-0));
console.log('---------------------------解构------------------')
let person = {
name:'test',
age:'123',
sex:{
man:1
}
};
// let {name,age} = person;
// console.log(name,age);
let {name:personName,age:personAge} = person;
console.log(personName,personAge);
let {sex:{man:sss}} = person;
console.log(sss)
let {name,test,age,sex} = person;
console.log(name,test,age,sex);
let {_} = null;
console