javascript-this Flashcards

1
Q

What is this in JavaScript?

A

implicit paramater in a js function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does it mean to say that this is an “implicit parameter”?

A

its there even though you don’t actually see it, its implied even though you can’t see it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When is the value of this determined in a function; call time or definition time?

A

the call time

in a definition, this has no value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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);
}
};

A

character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Given the above character object, what is the result of the following code snippet? Why?
character.greet();

A

the result is “it’s a me Mario”
greet is being called off of character so this will be character

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();

A

hello its a me, undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you tell what the value of this will be for a particular function or method definition?

A

you look to the left of the dot. in order to do this the function must be in the process of being called. if it is not being called and is only defined then the valure of this will be window. but that doesn’t matter, the point is that in this situation you have no control over the value of this which you need to have

How well did you know this?
1
Not at all
2
3
4
5
Perfectly