javascript继承澄清

Kin*_*rog 0 javascript inheritance

如何让最后一行工作?

function Animal(name){
    this.name = name;
}

Animal.prototype.speak = function(){
    alert('Hi, I\'m ' + this.name);
}

function Dog(name){
    this.name = name;
}

Dog.prototype.bark = function(){
  alert('Woof!');  
};

var fido = new Dog('Fido');
fido.bark(); // Woof!
fido.speak(); // Hi, I'm Fido *can't get this to work*
Run Code Online (Sandbox Code Playgroud)

mae*_*ics 7

您需要将Dog原型设置为新的Animal.

Dog.prototype = new Animal();
Run Code Online (Sandbox Code Playgroud)