Skip to content

Instantly share code, notes, and snippets.

@afg419
Forked from stevekinney/1510-function-cfus.md
Last active March 30, 2016 18:15
Show Gist options
  • Save afg419/1cb68db7ed6e448d5baef21708f3aa07 to your computer and use it in GitHub Desktop.
Save afg419/1cb68db7ed6e448d5baef21708f3aa07 to your computer and use it in GitHub Desktop.

JavaScript Functions

I can explain the difference between function declarations and function expressions.

  • function expressions are defined by var name = ... are invoked with name(...) but are not hoisted.
  • function declarations are defined by function name(...){...}, are invoked similarly, and are hoisted.

Rules for this:

    1. this is the global object (window, or global when in terminal)
    1. this is whatever object the function/method is being called from
    1. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment