Skip to content

Instantly share code, notes, and snippets.

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

JavaScript Functions

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment