Javascript继承,成员未定义
我正在一个名为Animation.js的文件中创建一个“类”:Javascript继承,成员未定义,javascript,class,constructor,prototype,prototypal-inheritance,Javascript,Class,Constructor,Prototype,Prototypal Inheritance,我正在一个名为Animation.js的文件中创建一个“类”: function Animation(s) { this.span = s; }; Animation.prototype = Object.create(Animation.prototype); Animation.prototype.constructor = Animation; function LinearAnimation(s, cP) { Animation.call(s); this.c
function Animation(s) {
this.span = s;
};
Animation.prototype = Object.create(Animation.prototype);
Animation.prototype.constructor = Animation;
function LinearAnimation(s, cP) {
Animation.call(s);
this.controlPoints = cP;
};
LinearAnimation.prototype = Object.create(Animation.prototype);
LinearAnimation.prototype.constructor = LinearAnimation;
我在一个名为LinearAnimation.js的文件中创建了一个子类:
function Animation(s) {
this.span = s;
};
Animation.prototype = Object.create(Animation.prototype);
Animation.prototype.constructor = Animation;
function LinearAnimation(s, cP) {
Animation.call(s);
this.controlPoints = cP;
};
LinearAnimation.prototype = Object.create(Animation.prototype);
LinearAnimation.prototype.constructor = LinearAnimation;
问题是,当我访问LinearName类中的
这个.span
成员时,它会说它是未定义的。我实施得好吗?谢谢。您想要动画。请拨打(此,s)
,而不是动画。请拨打(s)
。call
的第一个参数为函数调用设置this
。在您的代码中,您使用s
的this
调用Animation
,因此您的s
参数获得一个span
属性,而不是this
在linearanimation中构造的this
,函数将thisArg作为其第一个参数,表示被调用函数中的this
。在此之后,任何其他参数都将作为输入传递给被调用函数
此外,用从自身继承的对象替换函数(类)的原型也没有意义
试试这个:
函数动画{
这个.span=s;
};
函数线性化(s,cP){
动画。调用(此,s);
这是控制点=cP;
};
LinearAnimation.prototype=Object.create(Animation.prototype);
LinearAnimation.prototype.constructor=LinearAnimation;
var la=新的线性化('something',[1,2,3]);
控制台日志(la)代码>乍一看,您可能想要动画。调用(此,s)
,而不是动画。调用(s)
。call
的第一个参数为函数调用设置此
。Animation.prototype=Object.create(Animation.prototype)代码>可能也不是您想要的。