new 与 Object.create() 之间的真正区别是什么 [英] What is the real difference between new vs Object.create()
问题描述
我试图理解关键字 new
和 Object.create()
之间的真正区别
I am trying to understand the real difference between keywords new
and Object.create()
说明:
a1
是function
而a2
是object
a1
&a2
将key1
作为默认属性.key2
在从它们创建实例bxx
之后被分配.检查bxx
是独立对象本身还是只是一个引用.
a1
isfunction
whilea2
is anobject
a1
&a2
are havingkey1
as default property.key2
is being assigned after an instancebxx
is created from them. to check whetherbxx
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"
问题:
- 为什么不能在 a2 上使用
new
? - 为什么
b1obj.key1
是undefined
? - 为什么
b2obj.key2
仍然指的是父级的属性?
- Why can't use
new
on a2 ? - Why
b1obj.key1
isundefined
? - Why
b2obj.key2
is still referring to parent's property?
推荐答案
new
只能用于函数或类.当一个函数作为new User(...)
执行时,它会执行以下步骤:
new
can be used only with a function or a class. When a function is executed asnew 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.
<小时>
b1obj.key1
是undefined
因为Object.create
第一个参数是一个对象,它成为创建对象的原型.在这种情况下,它是a1
函数,它没有分配key1
属性,它只将key1
分配给它的this
调用时.
b1obj.key1
isundefined
becauseObject.create
first parameter is an object wich becomes a prototype of created object. In this case it is thea1
function, which doesn't have akey1
property assigned, it only assigneskey1
to itsthis
when called.
<小时>
b2obj
有a2
对象作为它的原型,所以它可以访问它的key2
属性
b2obj
has thea2
object as its prototype, so it has access to itskey2
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屋!