Skip to content

Instantly share code, notes, and snippets.

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

JavaScript Functions

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

-- Yep! A function declaration is parsed through by JS and hoisted to the top. It can actually be located at the bottom of the file, but able to be used and referenced beforehand due to hoisting. A function expression is more akin to a typical Ruby variable, it's name is hoisted to the top by not defined until the function expression is actually run.

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.

-- It refers to the object calling it.

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

-- use .call

I can explain the difference between call and apply.

-- Call takes in many arguments and can set 'this', but apply takes only 2, the second being an array of other arguments....?

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

-- Using something like an AJAX call, which is going to execute at a later time where the scope of 'this' may have changed from what we wanted or expected it to be.

I can explain how bind works.

-- Bind allows you to set what something like 'this' is for something which is running asynchronously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment