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

最新文章

  1. mybatis_映射查询
  2. Android开发笔记之《特斯拉-Tesla 代码分析》
  3. mininet和ryu控制器的连接
  4. Redis教程(三) list类型
  5. Thymeleaf+SpringMVC,如何从模板中获取数据
  6. 3到6年的.NETer应该掌握哪些知识?
  7. RAID与双机热备简单介绍与区别
  8. Javascript:常用函数封装
  9. Proxy模式
  10. SpringMvc多文件上传简单实现
  11. Cocos2d-x学习笔记之Cocos2d-x开发环境搭建
  12. rk3288 ov8858 camera移植
  13. .net程序员转战android第二篇---牛刀小试
  14. Win10更换电脑,又不想重装系统的解决方法
  15. BLDC
  16. python类型错误:can only concatenate list (not "str") to list
  17. php初级之数组与 类初级
  18. luogu P3690 【模板】Link Cut Tree (动态树)
  19. 使用Homebrew在Mac OS X EI Capitan上安装与配置nginx和PHP
  20. 关于Cocos2d-x中增加暂停按钮的步骤

热门文章

  1. js判断时间格式不能超过30天
  2. 【Python打包成exe方法】——已解决导入第三方包无法打包的问题
  3. 常⽤的meta标签有哪些
  4. MFC---典型类和函数
  5. spring原始注解
  6. drf过滤和排序及异常处理的包装
  7. java实现sftp文件上传下载
  8. 2021.08.16 P1260 工程规划(差分约束)
  9. Promise的then和catch如何影响状态的变化
  10. 【PostgreSQL】入门学习笔记