I can explain the difference between function declarations and function expressions.
- function expressions are defined by
var name = ...
are invoked withname(...)
but are not hoisted. - function declarations are defined by
function name(...){...}
, are invoked similarly, and are hoisted.
Rules for this
:
-
this
is the global object (window, or global when in terminal)
-
this
is whatever object the function/method is being called from
-
this
is whatever you say this is.
I can explain what the value of this
is in a normal function.
this
is the window itself.
I can explain what the value of this
is when called from the context of an object.
this
is the object.
I can explain how to explicitly set the value of this
in a function.
- the call method executes a function, but the first parameter is the new
this
.
I can explain the difference between call
and apply
.
- the apply method and call method are identical, except, call takes 2nd - nth parameter as parameters, while call takes these parameters as an array.
- note... in ruby you can use splat for this kind of thing. arry = [1,2,3], def method(x,y,z) x + y + z end, method(*arry) works. In ES6, method(...arry) also works.
I can describe an case where I might need to use bind
to avoid polluting the global scope.
I can explain how bind
works.
- calling bind on a function returns a brand new function, where the first param to bind sets
this
in new function, the latter params do partial function application.