javascript-this Flashcards
What is this in JavaScript?
the object that is currently executing the code.
What does it mean to say that this is an “implicit parameter”?
it is included as a paremeter even though it was not passed. It is implicit.
When is the value of this determined in a function; call time or definition time?
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);
}
};
character object
Given the above character object, what is the result of the following code snippet? Why?
character.greet()
It’s-a-me, Mario!
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
It’s-a-me, undefined!
How can you tell what the value of this will be for a particular function or method definition?
if there is no value to the left of the dot when the function is called, then by default, this will be the global window object.
How can you tell what the value of this is for a particular function or method call?
if there is no value to the left of the dot when the function is called, then by default, this will be the global window object.