EaselJS:有人可以解释演示中使用的继承模式吗? [英] EaselJS: Can somebody explain the inheritance pattern used in demos?

查看:212
本文介绍了EaselJS:有人可以解释演示中使用的继承模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EaselJS创建游戏,我想知道是否有人可以解释演示文件中使用的继承模式是如何工作的。具体来说,我正在查看以下文件: https://github.com/CreateJS /EaselJS/blob/master/examples/assets/Ship.js

I'm creating a game using EaselJS, and I'm wondering if somebody can explain how the inheritance pattern used in the demo files works. Specifically, I'm looking at the following file: https://github.com/CreateJS/EaselJS/blob/master/examples/assets/Ship.js

在第7行,Ship的原型设置为<$ c的实例$ c> createjs.container() ...

On line 7, the Ship's prototype is set to an instance of a createjs.container()...

var p = Ship.prototype = new createjs.Container();

然后在第28行,存储对原始构造函数的引用:

And then on line 28, a reference to the original constructor is stored:

p.Container_initialize = p.initialize;  //unique to avoid overiding base class

最后,发货对象在第30行初始化

Finally, the Ship object is initialized on line 30

p.initialize = function () {
    this.Container_initialize();

我正试图绕过这种模式,因为它与我来的不同在过去。有人可以向我解释为什么你会想要使用类的实例作为新类的原型?也许只是指向一个链接,解释这种模式?非常感谢任何帮助...我意识到这个问题有点模糊。

I'm trying to wrap my head around this pattern, because it is unlike anything I've come across in the past. Can somebody explain to me why you would want to use an instance of a class as a new class' prototype? Maybe just point me to a link with an explanation of this pattern? Any help here is greatly appreciated... I realize this question is a little bit vague.

推荐答案


我试图围绕这种模式,因为它与我过去遇到的任何事情不同。

I'm trying to wrap my head around this pattern, because it is unlike anything I've come across in the past.

我也不。它并没有太大的魔力,但他的结构绝对不常见。请参阅更正javascript继承以获得正确的模式。

Me neither. It doesn't do much magic, but he structure is definitely uncommon. See Correct javascript inheritance for the right pattern.

有人可以向我解释为什么你想要将一个类的实例用作新类的原型吗?

Can somebody explain to me why you would want to use an instance of a class as a new class' prototype?

你没有。您希望使用从父类的原型对象继承的对象。 不幸的是很多人使用新的ParConstructor 为此 - 如果构造函数为空,则效果很好。如果构造函数确实创建了实例属性或具有其他副作用,则可能会导致问题。但是,大多数人似乎并没有注意到或关心这一点。

You don't. You want to use an object that inherits from the parent class's prototype object. Unfortunately many people use new ParConstructor for that - which works well if the constructor function is empty. If the constructor does create instance properties or has other side effects, it can cause trouble. Most people don't seem to notice or care about that, though.


这种模式的解释?

explanation of this pattern?


function Ship() {
    this.initialize();
}


这只是调用在新实例上初始化方法( 这个 在构造函数中)。我认为直接在构造函数中放置初始化代码没有任何好处,但可能就是这样。

This just calls the initialize method on the new instance (this in a constructor). I don't see any benefits over placing the initialisation code directly in the constructor, but be that as it may.


var p = Ship.prototype = new createjs.Container();


如上所述,这是设置原型链继承来自 Container class的方法。它可能会进行不必要的实例初始化,因此应该用 Object.create 来电。它会为原型创建一个快捷方式变量。

As explained above, this is setting up the prototype chain to inherit methods from the Container "class". It probably does unnecessary instance initialisation, so it should be replaced with an Object.create call. And it creates a shortcut variable to the prototype.


// constructor:
p.Container_initialize = p.initialize;    //unique to avoid overiding base class


这里,一个明确的参考创建父级的构造函数。 p 上的初始化属性是从 Container <继承 / code> prototype,现在它被赋予 p 对象的自己的属性,并带有描述性名称。这是必要的,因为......

Here, an explicit reference to the parent's constructor is created. The initialize property on p is inherited from the Container prototype, and now it is made an own property of the p object with a descriptive name. That is needed because …


p.initialize = function () {
    this.Container_initialize();
    … // property init stuff


...这里有在原型对象上声明了自己的 initialize 方法,隐藏了继承的对象。现在,可以使用该专用属性在当前实例上调用超级初始化代码。这样做是相当 common ,但不是这种方法。相反, 致电 通常用于在子实例上应用父构造函数。

… here an own initialize method is declared on the prototype object, shadowing the inherited one. Still, the "super" initialisation code can now be invoked on the current instance using that dedicated property. Doing that is quite common, but not in this way with the method. Instead, call is normally used to apply the parent constructor on the child instance.

更好(至少更熟悉):

function Ship() {
    this.initialize();
}

var super_p = createjs.Container.prototype,
    p = Ship.prototype = Object.create(super_p);

p.initialize = function() {
    super_p.initialize.call(this);
    … // property init stuff

或者,没有初始化

function Ship() {
    createjs.Container.call(this);
    … // property init stuff
}
Ship.prototype = Object.create(createjs.Container.prototype);

这篇关于EaselJS:有人可以解释演示中使用的继承模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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