继承父构造函数的参数 [英] Inherit parent constructor arguments
问题描述
我在浏览讨论了类似的话题,但找不到我的情况...
我试图用参数调用父构造......似乎无法得到它的权利。
我有一个 PhysicsBody
超类需要阳极
作为其唯一的构造函数的参数:
函数PhysicsBody(阳极){
this.userData =阳极;
// ...
}
这一点 PhysicsBody
继承了 DynamicBody
类。是构造也需要阳极
作为唯一参数...喜欢我会在Java中做到这一点,我喜欢的东西相当于调用超级(阳极);
似乎无法找出如何
这里的 DynamicBody
类:
//想给新PhysicsBody(这一点,阳极),但失败!
DynamicBody.prototype =新PhysicsBody();
DynamicBody.prototype.constructor = DynamicBody;功能DynamicBody(阳极){ //调用父类的构造也失败了:
// PhysicsBody.prototype.constructor.call(这一点,阳极);
// ...
}
要做到这一点的方法之一:
函数PhysicsBody(阳极){
this.userData =阳极;
}PhysicsBody.prototype.pbMethod =函数(){};功能DynamicBody(阳极){
PhysicsBody.call(这一点,阳极);
}//设置继承
DynamicBody.prototype =的Object.create(PhysicsBody.prototype);DynamicBody.prototype.dbMethod =函数(){};
现在,当你做
VAR PB =新PhysicsBody('...');
实例 PB
得到了用户数据
属性,并且还继承了 PhysicsBody的方法。原型
( pbMethod
在这种情况下)。
当你做
变种DB =新DynamicBody('...');
实例分贝
得到了用户数据
属性,并且还继承了 DynamicBody的方法。原型
( dbMethod
在这种情况下),这反过来从 PhysicsBody.prototype
继承。
I'm browsing the discussion for a similar topic, but can't find my situation...
Am trying call parent constructors with parameters... can't seem to get it right.
I have a PhysicsBody
superclass that takes aNode
as its only constructor argument:
function PhysicsBody(aNode) {
this.userData = aNode;
// ...
}
Of this PhysicsBody
inherits a DynamicBody
class. Is constructor also takes aNode
as only argument... Like I would do it in Java, I'd love to call something equivalent to "super(aNode");
Can't seem to find out how.
Here's the DynamicBody
class:
// Wanted to give "new PhysicsBody(this, aNode)", but that fails!
DynamicBody.prototype = new PhysicsBody();
DynamicBody.prototype.constructor=DynamicBody;
function DynamicBody(aNode) {
// calling the parent constructor fails too:
// PhysicsBody.prototype.constructor.call(this, aNode);
//...
}
One way to do it:
function PhysicsBody( aNode ) {
this.userData = aNode;
}
PhysicsBody.prototype.pbMethod = function () {};
function DynamicBody( aNode ) {
PhysicsBody.call( this, aNode );
}
// setting up the inheritance
DynamicBody.prototype = Object.create( PhysicsBody.prototype );
DynamicBody.prototype.dbMethod = function () {};
Now, when you do
var pb = new PhysicsBody( '...' );
the instance pb
gets a userData
property and also inherits the methods from PhysicsBody.prototype
(pbMethod
in this case).
When you do
var db = new DynamicBody( '...' );
the instance db
gets a userData
property and also inherits the methods from DynamicBody.prototype
(dbMethod
in this case), which in turn inherits from PhysicsBody.prototype
.
这篇关于继承父构造函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!