Skip to content

Instantly share code, notes, and snippets.

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

JavaScript Functions

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

  • A function declaration is a named function that is not stored as a variable. An expression is an anonymous function set to a variable.
function logSomething(thing){
  console.log('something');
}```

```Expression:
var logSomething = function(something){
  console.log('something');
}```


I can use declarations above where they are written, expressions I can't. BAM.

I can explain what the value of `this` is in a normal function.
* `this` is the window/global object. Unless otherwise defined. 

I can explain what the value of `this` is when called from the context of an object.
* `this` is whatever object its being called from - whatever 'context' it's wrapped in. 

I can explain how to explicitly set the value of `this` in a function.
* `this` is whatever you say it is if you define it with `call` or `apply`. The developer always wins! 

I can explain the difference between `call` and `apply`.
* `call` allows you to redefine what 'this' is, and pass additional arguments into a function
* `apply` does the same thing but the additional arguments are an array.

I can describe an case where I might need to use `bind` to avoid polluting the global scope.
* Asynchronous things where you need to call `this` at a later time and really care about what `this` is. 

I can explain how `bind` works.
* `bind` is similar to `call` and `apply` however it doesnt immediately call the function, but holds onto it until it is needed and defines `this` as you see fit. 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment