js this Flashcards
What is this in JavaScript?
keyword for the object you are working with; it’s an implicit parameter
What does it mean to say that this is an “implicit parameter”?
it is available in a function’s code block even though it was never included in the function’s parameter list or declared
When is the value of this determined in a function; call time or definition time?
call time; by default this will be given the value of the global window object
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); } };
undefined since the func has not been called
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
It’s-a-me, Mario; it has been called
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!
window object
How can you tell what the value of this will be for a particular function or method definition?
not actively using the key thus unable to tell
How can you tell what the value of this is for a particular function or method call?
the object to the left of the dot
window for a general function