JavaScript继承:Object.create与new [英] JavaScript inheritance: Object.create vs new

查看:170
本文介绍了JavaScript继承:Object.create与new的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,这两个示例有什么区别:

In JavaScript what is the difference between these two examples:

先决条件:

function SomeBaseClass(){
}

SomeBaseClass.prototype = {
    doThis : function(){
    },

    doThat : function(){
    }
}

继承示例A使用Object.create:

Inheritance example A using Object.create:

function MyClass(){
}

MyClass.prototype = Object.create(SomeBaseClass.prototype);

继承示例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/three.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 对象不会有<$ h $ =http://jsfiddle.net/dhWmz/1/的 publicProperty 属性=noreferrer>在此示例中。

The obj object won't have publicProperty property like in this example.

MyClass.prototype = new SomeBaseClass();

它正在执行构造函数函数, 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();
    }
}

上面的代码只添加对象.create 函数如果它不可用,那么你可以使用 Object.create 函数,我认为上面的代码描述了对象.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屋!

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