28/30! Code runs, but it has error on my output


#1

My code runs but the output is not correct. It says: "ReferenceError: dog is not defined."
I don't know if my code is incorrect, but why it running?

This is my code:

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 " + dog.breed + "dog");
};

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

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


#2

In your =prototype= sayHello() Method
you should be using the this-keyword.
like
console.log("Hello this is a " + this.breed + "dog");

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

Below i have provided some references...but for now.
The ECMAScript Standard defines this as a keyword that "evaluates to the value of the ThisBinding of the current execution context" (ยง11.1.1)

var bark = function(){
         console.log("Woof Woof!!")
     };

//ThisBinding is set to ===> window
this.bark()
//or
window.bark()
//or
bark();

Then create an Instance of Dog via new on it's Class-constructor
and we call this Instance snoopy

Now we add a Method(is a function within an object)
and we name this METHOD bark.

The object snoopy now has 2 properties,
a breed property with property-key breed and it's associated string VALUE "Beagle"
a bark property with property-key bark and it's associated function VALUE

Now the bark Method can only be reached VIA the snoopy object

 console.log( this.snoopy );
 console.log( this.snoopy.bark.toString() );
 this.snoopy.bark();
 snoopy.bark();
function Dog (breed) {
          this.breed = breed;
      }
// here we make snoopy
var snoopy = new Dog("Beagle");
// we need you to teach snoopy how to bark here
snoopy.bark = function(){           //var bark = function()
          console.log("Woof Woof!!");
     };
console.log( this.snoopy );
console.log( this.snoopy.bark.toString() );
this.snoopy.bark();
snoopy.bark();

references:

http://javascript.crockford.com/survey.html
look under the header Objects and this
( also look at the code-conventions on www.crockford.com )

google search:
javascript this keyword site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

javascript this keyword explained site:stackoverflow.com
= http://stackoverflow.com/questions/3127429/javascript-this-keyword

And as you are starting to go in depth, you might want
to familiarize your self with closures
= http://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1

javascript closure site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures


#3

You forgot to add space :relaxed:

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