Javascript-this-Q&A Flashcards
What is this in JavaScript?
“This” is the reference of the lexical scope of the function at call time
What does it mean to say that this is an “implicit parameter”?
We dont specifically define it but it is still available
When is the value of this determined in a function; call time or definition 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 value of the firstName property of the character object
Given the above character object, what is the result of the following code snippet? Why? character.greet();
the console.log message (Its a me mario)
Given the above character object, what is the result of the following code snippet? Why? var hello = character.greet; hello();
undefended (its a me ‘undefined’)
How can you tell what the value of this will be for a particular function or method definition?
There is no way to know when it is not defined at definition time
How can you tell what the value of this is for a particular function or method call?
look at the lexical scope of where it is being called