Skip to content

Instantly share code, notes, and snippets.

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

JavaScript Functions

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

Yes.

function declaration - this entire function gets hoisted
function someThing(x) {
console.log("stuff");
}

function expression - only the name of the variable gets hoisted and will return undefined until the actual line of code is run
var someThing = function(x) {
console.log("stuff");
};

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

The global object / window

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

Whatever object it's being called from.

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

Yes, with .call(), .apply(), or bind()

  1. I can explain the difference between call and apply.

With .call() we send in whatever we want this to be and each of our arguments. With .apply() we also send in what we want this to be but can then send in an array that will get split into our individual argements.

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

With ansyncronous things - we bind this to the function we will use when we get all our data back.

  1. I can explain how bind works.

It hands us back a new function replacing this with a hard set value that we determine.

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