28. Hello? Yes, This Is Dog


#1


https://www.codecademy.com/courses/objects-ii/5/2?curriculum_id=506324b3a7dffd00020bf661#


I am wondering why I have to use this.breed rather than this[breed]? This is really throwing me off. Thanks in advance.


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

// add the sayHello method to the Dog class 
// so all dogs now can say hello

Dog.prototype.sayHello = function()
{
    console.log("Hello this is a " + this.breed + " dog")
}

var yourDog = new Dog("golden retriever");
yourDog.sayHello();

var myDog = new Dog("dachshund");
myDog.sayHello();


#2

@terasolver27749

the this-keyword explained
using Dog class-constructor and snoopy Instance of Dog
http://www.codecademy.com/forum_questions/54809212631fe9483b007e43

in this exercise they are trying to encourage you to think about

J a v a S c r i p t and it's == p r o t o t y p e - C h a i n ==

They want you to set up the chain:

        OBJECT
  Object.prototype
           ^
           |
  Cat.prototype ((meow Method))  Cat.prototype.meow =
           |                      function() { console.log("Meow!"); };
           ^         
           |          Cat Class-constructor
           |              with this constructor
           |              you can create Instance's of Cat
           |                |
           |                V
           |                |
           |              using new Cat("aName","aBreed");
           |                |
           |                V
           |                |
           |            /Instance's / 
           |            | of      |
           |            | Cat |
           ^     The Cat-Instance has access
           |     to Cat.prototype.meow Method
           |     via the Cat-prototype which is
           ^     -chained- to the Cat-prototype.
           |
      + - -+- - - - -+
      |              |
 cheshire          gary
 Instance         Instance
penguin with only a Animal prototype sayName method defined

http://www.codecademy.com/forum_questions/55a1ff5b9113cb0df10005f4#comment-55a3119e937676536e0005ea


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.