Skip to content

Instantly share code, notes, and snippets.

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

JavaScript Functions

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

  • Function declarations do not declare var with a name before the function prior to the function keyword like function expressions do. With the expression, you cannot make a call to a function that is declared later in the file or it will throw an error because only the var part is hoisted to the top of the file, but the value of that (which is going to be a function) is still undefined. All function declarations (named functions) will be hoisted to the top of the file, so you can use them even before you actually defined them in the file.

I can explain what the value of this is in a normal function.

  • global window

I can explain what the value of this is when called from the context of an object.

  • the object

I can explain how to explicitly set the value of this in a function.

  • You can use call to pass in an explicit 'this.' Call's first argument is whatever you want the value of 'this' to be.

I can explain the difference between call and apply.

  • Apply takes an array of what the 'other' (non-this) arguments are, while call just takes the arguments directly. Use apply if you already have everything in an array.

I can describe an case where I might need to use bind to avoid polluting the global scope.

  • If you actually use this to set a variable and this is the global window, then you've created a global variable (and not achieved your actual purpose). Instead you can use bind to ensure your function executes on the correct scope.

I can explain how bind works.

  • Returns a copy of the function with 'this' set to a specific value that you provide.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment