I can explain the difference between function declarations and function expressions.
- Declarations are hoisted to top priority, whereas function expressions are declared but not defined until that line is run.
I can explain what the value of this
is in a normal function.
- global object
I can explain what the value of this
is when called from the context of an object.
- that particular object
I can explain how to explicitly set the value of this
in a function.
- .call() & .apply()
I can explain the difference between call
and apply
.
- call and apply both explicitly set "this" with their first argument. Then call takes arguments individually while apply takes an array of arguments and only accepts two total arguments.
- ex: call('this', a, b, c)
- ex: apply('this', [a, b, c])
- WHEN WE CALL A FUNCTION
I can describe an case where I might need to use bind
to avoid polluting the global scope.
- returns a brand new function (copy) where 'this' is explicitly set.
- WHEN WE DEFINE A FUNCTION
I can explain how bind
works.
- function() {}.bind(this)
- passes this in using current scope, especially helpful for asynchronous code