1

I'm figuring out prototypes in JS, and I can't figure for the life of me why this doesn't work:

var Mammal = {
    legs: 4
}

function Dog(color, soundItMakes) {
    this.prototype = Mammal;
    this.color = color;
    this.soundItMakes = soundItMakes;
    this.woof = function() { return this.soundItMakes; }
}

aDog = new Dog("brown", "beep beep!");
document.write(Mammal.legs + "<br>");
document.write(aDog.color + "<br>" + aDog.woof() + "<br>" + aDog.legs);

The first document.write() returns 4 as would be expected, but the second returns undefined for aDog.legs. Any advice would be a huge help.

dcgenjin
  • 1,108
  • 3
  • 12
  • 25
  • 1
    I recommend to read [MDN - Introduction to Object-Oriented JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript). – Felix Kling Mar 29 '15 at 21:56
  • 1
    Related (duplicate?): [Relation between \[\[Prototype\]\] and prototype in JavaScript](http://stackoverflow.com/q/383201/1529630) – Oriol Mar 29 '15 at 22:00
  • Sorry, added clarification of what was returning undefined – dcgenjin Mar 29 '15 at 22:17
  • 1
    There's nothing special about a `.prototype` property on a plain object. Only on functions. The way you have it right now, you'd have to do `aDog.prototype.legs`, which wouldn't be very desirable. –  Mar 29 '15 at 22:17
  • Maybe the following answer can help explaining what prototype is and how it's used. As mentioned before; prototype only does something if you set it's value on a constructor function (normal function used to construct instances). There are other ways to create instances and set it's prototype chain like Object.create. http://stackoverflow.com/a/16063711/1641941 – HMR Mar 30 '15 at 01:45

1 Answers1

1

Right now prototype is the own property of the Dog instance object. So if you want you could access it like aDog.prototype.legs. However, this is not the same as setting Dog constructor prototype.

Your code should be this:

var Mammal = {
    legs: 4
}

function Dog(color, soundItMakes) {
    this.color = color;
    this.soundItMakes = soundItMakes;
    this.woof = function() { return this.soundItMakes; }
}

Dog.prototype = Mammal;
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • I see what you mean about Mammal being the prototype of the INSTANCE of Dog, but I still don't quite understand why that should prevent the instance from having access to a property of its prototype... – dcgenjin Mar 29 '15 at 22:18
  • @somecbusnerd: Because having a `.prototype` property doesn't mean an object inherits from that. – Bergi Mar 29 '15 at 22:19
  • 1
    @somecbusnerd: You have to understand that prototype is a special object member that is applied to the "class", and not any specific instance. In your original example, try accessing the legs member via aDog.prototype.legs and you should see the correct result. This is because "prototype" is treated as any other member variable when assigned to an instance. Only when assigned to a class (i.e. Dog.prototype = Mammal) will it utilize the special inheritance behavior. – Evan Steinkerchner Mar 29 '15 at 22:23