javascript-this Flashcards
What is this in JavaScript?
It is an implicit parameter of all JavaScript functions.
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 with var
When is the value of this determined in a function; call time or definition time?
the value of this is determined when the function is called
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); } };
there’s no this yet; it’s not being called.
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
I
t’s-a-me, Mario!’
Because the var ‘character’ is being passed as the obj.
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
Undefined; because of window is default value for this.
How can you tell what the value of this will be for a particular function or method definition?
if you cannot see the function being called, then you do not know what the value of this will be.
How can you tell what the value of this is for a particular function or method call?
Look for an object to the left of the dot.