This forum is now read-only. Please use our new forums! Go to forums
12/30 Teach Snoopy: My code is a bit different but runs. Could somebody explain it?
The correct code that we are supposed to write is this:
function Dog (breed) {
this.breed = breed;
}
// here we make buddy and teach him how to bark
var buddy = new Dog("Golden Retriever");
buddy.bark = function() {
console.log("Woof");
};
buddy.bark();
// 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!!");
};
// this causes an error, because snoopy doesn't know how to bark!
snoopy.bark(); //this.bark();
If I change just two lines. (Commented out in the above program), the code works. How?
function Dog (breed) {
this.breed = breed;
}
// here we make buddy and teach him how to bark
var buddy = new Dog("Golden Retriever");
buddy.bark = function() {
console.log("Woof");
};
buddy.bark();
// here we make snoopy
var snoopy = new Dog("Beagle");
// we need you to teach snoopy how to bark here
var bark = function(){
console.log("Woof Woof!!");
};
// this causes an error, because snoopy doesn't know how to bark!
this.bark();
Also why does the following not work:
var bark = function(){
console.log("Woof Woof!!")
};
snoopy.bark();
Answer 548140209113cb27c7000536
@RDnatty,
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
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency