Javascript继承,成员未定义

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

我正在一个名为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.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)可能也不是您想要的。