JavaScript 继承:Object.create 与 new [英] JavaScript inheritance: Object.create vs new
问题描述
在 JavaScript 中,这两个示例之间的区别是什么:
In JavaScript what is the difference between these two examples:
先决条件:
function SomeBaseClass(){
}
SomeBaseClass.prototype = {
doThis : function(){
},
doThat : function(){
}
}
使用 Object.create 的继承示例 A:
Inheritance example A using Object.create:
function MyClass(){
}
MyClass.prototype = Object.create(SomeBaseClass.prototype);
使用 new 关键字的继承示例 B
Inheritance example B using the new keyword
function MyClass(){
}
MyClass.prototype = new SomeBaseClass();
这两个例子似乎都在做同样的事情.你什么时候会选择一个?
Both examples seem to do the same thing. When would you chose one over the other?
另外一个问题:考虑下面链接(第 15 行)中的代码,其中对函数自己的构造函数的引用存储在原型中.为什么这很有用?
An additional question: Consider code in below link (line 15), where a reference to the the function's own constructor is stored in the prototype. Why is this useful?
https://github.com/mrdoob/三.js/blob/master/src/loaders/ImageLoader.js
摘录(如果您不想打开链接):
Excerpt (if you don't want to open the link):
THREE.ImageLoader.prototype = {
constructor: THREE.ImageLoader
}
推荐答案
在你的问题中你提到两个例子似乎都在做同样的事情
,这根本不是真的,因为
In your question you have mentioned that Both examples seem to do the same thing
, It's not true at all, because
function SomeBaseClass(){...}
SomeBaseClass.prototype = {
doThis : function(){...},
doThat : function(){...}
}
function MyClass(){...}
MyClass.prototype = Object.create(SomeBaseClass.prototype);
在这个例子中,你只是继承了 SomeBaseClass'prototype
但是如果你的 SomeBaseClass
中有一个属性,比如
In this example, you are just inheriting SomeBaseClass' prototype
but what if you have a property in your SomeBaseClass
like
function SomeBaseClass(){
this.publicProperty='SomeValue';
}
如果你喜欢使用它
var obj=new MyClass();
console.log(obj.publicProperty); // undefined
console.log(obj);
obj
对象不会像 publicProperty 属性>在这个例子中.
The obj
object won't have publicProperty
property like in this example.
MyClass.prototype = new SomeBaseClass();
它正在执行constructor
函数,创建SomeBaseClass
的实例并继承整个SomeBaseClass
对象.所以,如果你使用
It's executing the constructor
function, making an instance of SomeBaseClass
and inheriting the whole SomeBaseClass
object. So, if you use
var obj=new MyClass();
console.log(obj.publicProperty); // SomeValue
console.log(obj);
在这种情况下,它的 publicProperty
属性也可用于 obj
对象,例如 在这个例子中.
In this case its publicProperty
property is also available to the obj
object like in this example.
由于 Object.create
在某些旧浏览器中不可用,在这种情况下您可以使用
Since the Object.create
is not available in some old browsers, in that case you can use
if(!Object.create)
{
Object.create=function(o){
function F(){}
F.prototype=o;
return new F();
}
}
上面的代码只是添加了 Object.create
函数,如果它不可用,那么你可以使用 Object.create
函数,我认为上面的代码描述了 Object.create
实际上确实如此.希望它会以某种方式有所帮助.
Above code just adds Object.create
function if it's not available so you can use Object.create
function and I think the code above describes what Object.create
actually does. Hope it'll help in some way.
这篇关于JavaScript 继承:Object.create 与 new的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!