JavaScript Object学习笔记二

Object.create(proto, [propertiesObject])//创建对象,使用参数一来作为新创建对象的__proto__属性,返回值为在指定原型对象上添加自身属性后的对象

//参数proto:必须,新对象的原型对象,可以是null/Object/函数的prototype属性,创建空对象时需传null

//参数[propertiesObject]:可选,添加到自身的可枚举属性,不是添加在原型链上的

 

Object.create()与new Object()的区别:

new Object()通过构造函数创建对象,添加的属性是在自身实例下的

var obj1 = {name: 'abc'};

var obj2 = new Object(obj1);//{name: 'abc'}  //obj2.__proto__={}

var obj3 = Object.create(obj1);//{}  //obj3.__proto__={name: 'abc'}

obj3.name='abc'//先访问自身属性,找不到再往下找原型上是否存在,不存在则返回undefined

创建空对象时不同

new Object();//{__proto__: Object}//是有原型属性的

Object.create(null);//{}//是没有原型属性的

 

参数[propertiesObject]添加的自身属性是不可写、不可枚举、不可配置的

var obj4 = Object.create({}, {name: {value: 'abc'}});//{name: 'abc'}

obj4.name = 'def';//obj4.name = 'abc';//不可写

obj4.age = '10'//{name: 'abc', age: '10'}

for(let key in obj4){console.log(key);}//age//不可枚举

delete obj4.name;//false//不可配置

 

参数[propertiesObject]创建的非空对象的属性描述符默认为false,而构造函数或者字面量方式创建的对象或者Object.create(null)创建的空对象添加的属性的属性描述符默认为true

var obj5 = Object.create({}, {a: {value: 1}});

Object.getOwnPropertyDescriptors(obj5);

// {a: {value: 1, configurable: false, enumerable: false, writable: false}}

var obj6 = Object.create(null); obj6.a = 1;

Object.getOwnPropertyDescriptors(obj6);

// {a: {value: 1, configurable: true, enumerable: true, writable: true}}

var obj7 = new Object({a: 1});

Object.getOwnPropertyDescriptors(obj7);

// {a: {value: 1, configurable: true, enumerable: true, writable: true}}

 

Object.setPrototypeOf(object, prototype)//设置对象的prototype对象,返回参数对象本身

Object.setPrototypeOf({x: 1}, {x: 2});//{x: 1, __proto__: {x: 2}}

 

Object.getPrototypeOf(object)//获取对象的原型对象

Object.getPrototypeOf(Object.setPrototypeOf({x: 1}, {x: 2}));//{x: 2}

Object.getPrototypeOf('abc') === String.prototype

Object.getPrototypeOf(true) === Boolean.prototype

 

Object.getOwnPropertyDescriptor(object, prop)//返回object对象prop属性的描述对象

Object.getOwnPropertyDescriptor({a: 1}, 'a')

//{value: 1, configurable: true, enumerable: true, writable: true}

 

Object.getOwnPropertyDescriptors(object)//返回object对象自身所有可枚举属性及描述对象

var obj8 = Object.create({x: 1}, {y: {value: 2}});

Object.getOwnPropertyDescriptors(obj8);

//{y: {value: 2, configurable: false, enumerable: false, writable: false}}

 

Object.assign()[详细请见这篇文章]不能拷贝原型上的属性或方法以及不能正确拷贝属性的get/set方法

可以结合使用Object.create()、Object.setPrototypeOf()、Object.getPrototypeOf()、Object.getOwnPropertyDescriptors()来实现原型上的属性方法的拷贝,替代只有浏览器环境部署的__proto__实现的原型链继承

var obj1 = {a: 1}; 

function fn(){this.color = 'red';}
Object.assign(fn.prototype, obj1);

var obj2 = new fn();//{color: 'red', __proto__: {a: 1}}

var obj3 = Object.assign({}, obj2);//obj3.color='red'; obj3.a=undefined

 

var obj4 = Object.assign(Object.create(Object.getPrototypeOf(obj2)), obj2);

//obj4.color='red'; obj4.a=1;//这种方法不能拷贝get/set方法

 

var obj5 = Object.create(Object.getPrototypeOf(obj2), Object.getOwnPropertyDescriptors(obj2));

//obj5.color='red'; obj5.a=1;//可以拷贝到属性的get/set方法

 

Object.is()//判断两个值是否严格相等

Object.is('a', 'a')//true    Object.is({}, {})//false

弥补ES5对于+0与-0的判断以及NaN与NaN的判断

+0 === -0//true    NaN === NaN//fakse

Object.is(+0, -0)//false    Object.is(NaN, NaN)//true

posted @ 2021-05-15 17:33  IT小猿人  阅读(46)  评论(0编辑  收藏  举报