js this Flashcards
What is this in JavaScript?
the current object/context
What does it mean to say that this is an “implicit parameter”?
doesn’t need to be declared or included as parameter
When is the value of this determined in a function; call time or definition time?
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); } };
window
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
logs “It’s-a-me, Mario!” to the console. calling the greet method of the character object, so this is the character object
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet; hello();
logs “It’s-a-me, undefined!” to the console. calling the hello method of the window object, so this is the window object, and this.firstName is undefined
How can you tell what the value of this will be for a particular function or method definition?
the object that contains the function/method defintion
How can you tell what the value of this is for a particular function or method call?
look to the left of the dot. If nothing there, this is probably the window object