Differance between this this function and prototype


#1

So..

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();

Why not

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

// add the sayHello method to the Dog class 
// so all dogs now can say hello
**this.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();

I understand its stupid question but i just got lost.


#2

@urketadic,

Read the article about the this-keyword
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

thus if you define

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

you are actually defining

window.sayHello = function () {
       console.log ("Hello this is a " + this.breed + "dog" );

or

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

=============================================================

But 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