Objects function 'this' Flashcards

1
Q

When a function is invoked a new ________ context is created.

A

execution

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

Every time a _____ is run, ‘this’ variable is created.

A

function

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

Argument

A

The parameters you pass to a function

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

Does JavaScript pass variables by reference or by value?

A

Yes, primitive types are passed by value.

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

Primitive types are passed by ______. and Objects are passed by ______.

A

reference

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

What does passed-by value mean?

A

If you change the value of a primitive type inside a function, the changes won’t affect the variable in the outer scope. Because you passed a copy set in different locations.

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

What does passed-by reference mean?

A

You’re passing something that points to something else vs the copy. Any changes made to that reference will also be changed in the outer scope.

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

== is called?

A

double equal

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

=== is called?

A

strict equal

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

strict equal checks for?

A

both type as well as value equality

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

double equal checks for?

A

only checks for the equality of the values.

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

Why does the js engine produce “true” for 0==’0’?

A

A double equality only checks for the value so the engine tries to convert the number on the left side of the equal sign to a string.

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

How do you check if a type is a number?

A

isNaN();

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

NaN == NaN produces and why?

A

false. type coercion. It’s coercing NaN into a string to check if its a number.

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

undefined == null produces what result.

A

True. undefined is null and null is undefined

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

console.log(typeof(null)); produces what result?

A

“object”. JavaScript incorrectly reports the type of null as object.

17
Q

console.log(NaN == “1”);

A

NaN equal to ANYTHING is always false, even when compared to itself. (one of those weird moments in JS)

18
Q

the “this” keyword is determined by the ______ context.

A

calling. It’s determined by the way in which a function is called.

19
Q

“this” is strict mode is always

A

undefined

20
Q

A method is a function attached to an ______.

A

object

21
Q

this is lexically bound, which means its value is derived from the ____________.

A

context where they are defined.

22
Q

Can you bind an existing object method to remap its ‘this’ value?

A

Yes

23
Q
const car = {
  maker: 'Ford',
  model: 'Fiesta',
  drive() {
    console.log(`Driving a ${this.maker} ${this.model} car!`)
  }
}
const anotherCar = {
  maker: 'Audi',
  model: 'A4'
}

What does this result in?

A

Driving a Audi A4 car!

24
Q
const car = {
  maker: 'Ford',
  model: 'Fiesta'
}
const drive = function() {
  console.log(`Driving a ${this.maker} ${this.model} car!`)
}.bind(car)
A

Driving a Ford Fiesta car!

25
Q

The first parameter you pass to call() or apply() is always bound to _____.

A

‘this’.

26
Q

The difference between call() and apply() is …

A

just that the apply() wants an array as the arguments list, while call() accepts a variable number of parameters, which passes as function arguments.

27
Q
const car = {
  maker: 'Ford',
  model: 'Fiesta'
}
const drive = function(kmh) {
  console.log(`Driving a ${this.maker} ${this.model} car at ${kmh} km/h!`)
}
drive.call(car, 100)

Results in?

A

Driving a Ford Fiesta car at 100 km/h!