0

How do I get the last line to work?

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*
4
  • possible duplicate of Javascript: prototypal inheritance Commented Nov 20, 2013 at 21:31
  • There is no inheritance with Animal in your code Commented Nov 20, 2013 at 21:33
  • You've done absolutely nothing to link your Dog and Animal functions. How is it you expect this to work? What have you tried? Commented Nov 20, 2013 at 21:47
  • @meagar I think this is his question... how to link them ? ;) Commented Nov 20, 2013 at 21:49

3 Answers 3

7

You need to set the Dog prototype to a new Animal.

Dog.prototype = new Animal();
Sign up to request clarification or add additional context in comments.

5 Comments

Do I need to set Dog.prototype.constructor = Dog;
@KingKongFrog: for your use case you do not need to; however, doing so might be a good idea in case other code checks that property for type identification. In practice I've never seen an important reason to do so.
this is where my main confusion is. Why/when do I have to set the prototype.constuctor ?
@KingKongFrog: that is a totally different (and totally valid) question.
This is not a good idea too use an instence of the parent object has prototype. The parent must be constructible without parameter. And the constructor may certainly do useless operation for prototype declaration.
1
...
function Dog(name){
    this.name = name;
}

Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor= Dog;

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

Object.create create a new empty object and use the parameter has prototype of this object.

You can make a inheritence in the constructor too:

function Dog(name){
    Animal.call(this, name);
    this.name = name;
}

Here, you call the Animal constructor on the new Dog context.

2 Comments

In this specific exemple the inheritence in the constructor is a little bit useless.
In this specific example it's actually super useful. It demonstrates how instance specific member name gets initialized as instanceOfDog.name when you create it. this.name=name is no longer needed in Dog because Animal already does that. If you have a whole bunch of values that need to be validated and set then one line in Dog, Cat, Bird ... would take care of that.
1

In short; you can do it like this:

var Dog = function(name){
  //re use Animal constructor
  Animal.call(this,name);
  //you can do Animal.apply(this,arguments); as well
};
//set up the prototype chain
Dog.prototype = Object.create(Animal.prototype);
//repair the constructor property
Dog.prototype.constructor = Dog;

An introduction to constructor functions, inheritance and prototype can be found here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.