I can explain the difference between function declarations and function expressions.
- Function expressions store them in variables. Function declarations start with "function [name]" and are hoisted.
I can explain what the value of this
is in a normal function.
- Should refer to the function in the context it's being run in. This is the window "normally".
I can explain what the value of this
is when called from the context of an object.
- It should refer to the object based on the context it was called from.
I can explain how to explicitly set the value of this
in a function.
- Can be controlled with .call() or .apply() for example. These allow you to pass the value of 'this' as a first argument.
I can explain the difference between call
and apply
.
- Apply takes the arguments as an array.
I can describe an case where I might need to use bind
to avoid polluting the global scope.
- Asynchronous cases where we want to call the function later.
I can explain how bind
works.
- Bind is similar to apply but sets 'this' when the function is defined, rather than when it's called.