new 与 Object.create() 之间的真正区别是什么 [英] What is the real difference between new vs Object.create()

查看:28
本文介绍了new 与 Object.create() 之间的真正区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解关键字 newObject.create() 之间的真正区别

I am trying to understand the real difference between keywords new and Object.create()

说明:

  • a1functiona2object
  • a1 &a2key1 作为默认属性.key2 在从它们创建实例 bxx 之后被分配.检查 bxx 是独立对象本身还是只是一个引用.
  • a1 is function while a2 is an object
  • a1 & a2 are having key1 as default property. key2 is being assigned after an instance bxx is created from them. to check whether bxx is independent object itself or just a reference.

"use strict";

function a1() {
  this.key1 = "value 1";
}

let a2 = {
  key1: "value 1"
};

let b1new, b1obj, b2new, b2obj;

try {
  b1obj = Object.create(a1);
} catch (e) {
  console.error("Error a1: ", e.message)
}
try {
  b1new = new a1();
} catch (e) {
  console.error("Error a1: ", e.message)
}


try {
  b2obj = Object.create(a2);
} catch (e) {
  console.error("Error a2: ", e.message)
}
try {
  b2new = new a2();
} catch (e) {
  console.error("Error a2: ", e.message)
}

//let b = new a();

a1.key2 = "value 2";
a2.key2 = "value 2";

if (b1obj) {
  console.log("b1obj.key1: ", b1obj.key1);
  console.log("b1obj.key2: ", b1obj.key2);
}
if (b1new) {
  console.log("b1new.key1: ", b1new.key1);
  console.log("b1new.key2: ", b1new.key2);
}
if (b2obj) {
  console.log("b2obj.key1: ", b2obj.key1);
  console.log("b2obj.key2: ", b2obj.key2);
}
if (b2new) {
  console.log("b2new.key1: ", b2new.key1);
  console.log("b2new.key2: ", b2new.key2);
}

输出:

"Error a2: " "a2 is not a constructor"    
"b1obj.key1: " undefined    
"b1obj.key2: " "value 2"    
"b1new.key1: " "value 1"    
"b1new.key2: " undefined    
"b2obj.key1: " "value 1"    
"b2obj.key2: " "value 2"

问题:

  1. 为什么不能在 a2 上使用 new ?
  2. 为什么 b1obj.key1undefined ?
  3. 为什么 b2obj.key2 仍然指的是父级的属性?
  1. Why can't use new on a2 ?
  2. Why b1obj.key1 is undefined ?
  3. Why b2obj.key2 is still referring to parent's property?

推荐答案

  1. new 只能用于函数或类.当一个函数作为 new User(...) 执行时,它会执行以下步骤:

  1. new can be used only with a function or a class. When a function is executed as new User(...), it does the following steps:

  • 一个新的空对象被创建并分配给this.

函数体执行.通常它会修改this,为其添加新属性.

The function body executes. Usually it modifies this, adds new properties to it.

如果没有明确的return语句,则返回this的值.

If there is no explicit return statement, the value of this is returned.

<小时>

  1. b1obj.key1undefined 因为 Object.create 第一个参数是一个对象,它成为创建对象的原型.在这种情况下,它是 a1 函数,它没有分配 key1 属性,它只将 key1 分配给它的 this 调用时.
  1. b1obj.key1 is undefined because Object.create first parameter is an object wich becomes a prototype of created object. In this case it is the a1 function, which doesn't have a key1 property assigned, it only assignes key1 to its this when called.

<小时>

  1. b2obja2 对象作为它的原型,所以它可以访问它的 key2 属性
  1. b2obj has the a2 object as its prototype, so it has access to its key2 property

总结:虽然 new 关键字更常用于从现有的模板"创建对象的新实例,Object.create更灵活,并允许您使用原型,创建属性描述符.例如,您可以创建对象的浅拷贝:

Suming up: while new keyword is more often used to create new instances of objects from an existing 'template', Object.create is more flexible and allows you to work with a prototype, create property descriptors. For example, you can create a shallow copy of object:

let clone = Object.create(Object.getPrototypeOf(obj), 
                          Object.getOwnPropertyDescriptors(obj));

我建议您阅读这篇关于new关键字和this 关于 Object.create 和其他原型方法.

I suggest you to read this article about new keyword and this one about Object.create and other prototype methods.

UPDATE(关于对象与其原型的关系):创建新对象后,更改其属性不会影响原型.例如,

UPDATE (about the relationship between object and its prototype): Once you have created a new object, changing its properties does not affect the prototype. For example,

let a2 = {
    key2: "some text"
};
let b2 = Object.create(a2);
a2.key2 = "I am a2";
b2.key2 = "I am b2";

alert(a2.key2 + ", " + b2.key2);

会提醒我是a2,我是b2.这是因为 b2 有它自己的 key2 属性.但是如果没有,JavaScript 会在它的原型中寻找它.您可以在此处

will alert I am a2, I am b2. That's because b2 has its own key2 property. But If it doesn't, JavaScript will look for it in its prototype. You can find a detailed description of the prototype inheritance here

这篇关于new 与 Object.create() 之间的真正区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆