javascript-this Flashcards
What is this in JavaScript?
“this” is the the object to the left of the dot when the function is called(as a method).
What does it mean to say that this is an “implicit parameter”?
it means that it’s available in the function code block even it’s not included in the parameter list or declared as a variable.
When is the value of this determined in a function; call time or definition time?
the value of “this” is determined at call time.
What does this refer to in the following code snippet? var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
the default window object
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
the message in the greet method of character object will show because the greet method of the character object is called.
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
the result would only show the string part, It’s-a-me, and this.firstName would be undefined!
the firstName is undefined because no object is appointed when the hello function is called.
How can you tell what the value of this will be for a particular function or method definition?
you don’t know the value yet
How can you tell what the value of this is for a particular function or method call?
you can look for the object to the left of the dot when a function or method is called.