Skip to content

Instantly share code, notes, and snippets.

@adamki
Forked from stevekinney/1508-functions-cfu.md
Last active February 3, 2016 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamki/2f8cc47a96fab07ee806 to your computer and use it in GitHub Desktop.
Save adamki/2f8cc47a96fab07ee806 to your computer and use it in GitHub Desktop.

JavaScript Functions

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

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

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

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

I can explain the difference between call and apply. yes

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

I can explain how bind works. Maybe. I got confused with trhe last section of the lesson. see:

Function.prototype.bindTo = function (context) {
  var fn = this;
  return function () {
    fn.apply(context, arguments);
  }
}

function logThisAndStuff(stuff) {
  console.log(this, stuff);
}

var logSandwich = logThis.bindTo('sandwich');
var logTaco = logThis.bindTo('taco');
var logBurrito = logThis.bindTo('burrito');

logSandwich('one', 'two', 'three'); // 'sandwich', 'one', 'two', 'three'
logTaco('one', 'two', 'three'); // 'taco', 'one', 'two', 'three'
logBurrito('one', 'two', 'three'); // 'burrito', 'one', 'two', 'three'
All functions have access to an array-like collection called arguments. In the example above, we swap out call() for apply() and let it spread the collections of arguments passed in to our explicitly-bound function a

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