Object.create()和new Object()

Object.create(null) 创建的对象是一个空对象,在该对象上没有继承 Object.prototype 原型链上的属性或者方法,例如:toString(), hasOwnProperty()等方法
Object.create()方法接受两个参数:Object.create(obj,propertiesObject);
  obj:一个对象,应该是新创建的对象的原型。
  propertiesObject:可选.
使用Object.create()是将对象继承到__proto__属性上
  var test = Object.create({x: 123, y: 345});
  console.log(test);
  console.log(test.x)
  console.log(test.__proto__.x)
  console.log(test.__proto__.x === test.x);

  var test1 = new Object({x: 123, y: 345});
  console.log(test1);
  console.log(test1.x);
  console.log(test1.__proto__.x)
  console.log(test1.__proto__.x === test1.x);

  var test2 = {x: 123, y: 345};
  console.log(test2);
  console.log(test2.x);
  console.log(test2.__proto__.x);
  console.log(test2.__proto__.x === test2.x)

posted @ 2018-03-13 16:41  麦芽无心  阅读(231)  评论(0编辑  收藏  举报