Javascript继承,成员未定义

Ped*_*osa 0 javascript constructor prototype class prototypal-inheritance

我正在一个名为Animation.js的文件中创建一个"类":

function Animation(s) {
  this.span = s;
};

Animation.prototype = Object.create(Animation.prototype);
Animation.prototype.constructor = Animation;
Run Code Online (Sandbox Code Playgroud)

我创建了一个名为LinearAnimation.js的子类:

function LinearAnimation(s, cP) {
     Animation.call(s);
     this.controlPoints = cP;
};

LinearAnimation.prototype = Object.create(Animation.prototype);
LinearAnimation.prototype.constructor = LinearAnimation;
Run Code Online (Sandbox Code Playgroud)

问题是,当我访问this.spanLinearAnimation类中的成员时,它说它是undefined.我实施得好吗?谢谢.

Ami*_*mit 5

Function.prototype.call()函数将thisArg作为它的第一个参数,意思是被this调用函数内部.之后,任何其他参数都是(are ..)作为输入传递给被调用函数.

另外,用从自身继承的对象替换函数(类)的原型是没有意义的.

试试这个:

function Animation(s) {
  this.span = s;
};

function LinearAnimation(s, cP) {
     Animation.call(this, s);
     this.controlPoints = cP;
};
LinearAnimation.prototype = Object.create(Animation.prototype);
LinearAnimation.prototype.constructor = LinearAnimation;

var la = new LinearAnimation('something', [1, 2, 3]);

console.log(la);
Run Code Online (Sandbox Code Playgroud)