Objects function 'this' Flashcards
When a function is invoked a new ________ context is created.
execution
Every time a _____ is run, ‘this’ variable is created.
function
Argument
The parameters you pass to a function
Does JavaScript pass variables by reference or by value?
Yes, primitive types are passed by value.
Primitive types are passed by ______. and Objects are passed by ______.
reference
What does passed-by value mean?
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.
What does passed-by reference mean?
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.
== is called?
double equal
=== is called?
strict equal
strict equal checks for?
both type as well as value equality
double equal checks for?
only checks for the equality of the values.
Why does the js engine produce “true” for 0==’0’?
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 do you check if a type is a number?
isNaN();
NaN == NaN produces and why?
false. type coercion. It’s coercing NaN into a string to check if its a number.
undefined == null produces what result.
True. undefined is null and null is undefined
console.log(typeof(null)); produces what result?
“object”. JavaScript incorrectly reports the type of null as object.
console.log(NaN == “1”);
NaN equal to ANYTHING is always false, even when compared to itself. (one of those weird moments in JS)
the “this” keyword is determined by the ______ context.
calling. It’s determined by the way in which a function is called.
“this” is strict mode is always
undefined
A method is a function attached to an ______.
object
this is lexically bound, which means its value is derived from the ____________.
context where they are defined.
Can you bind an existing object method to remap its ‘this’ value?
Yes
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?
Driving a Audi A4 car!
const car = { maker: 'Ford', model: 'Fiesta' } const drive = function() { console.log(`Driving a ${this.maker} ${this.model} car!`) }.bind(car)
Driving a Ford Fiesta car!
The first parameter you pass to call() or apply() is always bound to _____.
‘this’.
The difference between call() and apply() is …
just that the apply() wants an array as the arguments list, while call() accepts a variable number of parameters, which passes as function arguments.
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?
Driving a Ford Fiesta car at 100 km/h!