Last active
October 14, 2016 16:47
-
-
Save kimniche/3e76e61025c1e08d590b193e510aa9e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://benalman.com/news/2010/11/immediately-invoked-function-expression/ | |
// defining functions | |
// 1. function declaration | |
function f1() { | |
console.log('i do declare'); | |
} | |
// 2. function expression | |
var f2 = function(foo) { | |
console.log('expre$$ed ' + foo); | |
} | |
// invoking functions | |
f1(); | |
f2('foo'); // -> expre$$ed foo | |
// "privacy" | |
function talkingDog() { | |
var name = 'wilfred'; | |
return function() { | |
console.log('Sup I\'m a dog named ' + name); | |
} | |
} | |
var wilfred = talkingDog(); | |
wilfred(); // -> Sup I'm a dog named wilfred | |
// enter IIFE | |
(function(){ /* code */ }()); | |
(function(){ /* code */ })(); | |
// example without arg | |
var wilfred = (function() { | |
var name = 'wilfred'; | |
return { | |
speak: function() { | |
console.log('my name is ' + name); | |
} | |
} | |
})(); | |
wilfred.speak(); // -> my name is wilfred | |
// example with arg | |
var friend = (function(first, last) { | |
return { | |
greet: function() { | |
console.log('greetings! i\'m ' + first + ' ' + last); | |
} | |
} | |
})('ted', 'mosby'); | |
friend.greet(); // -> hey i'm ted mosby | |
// in our code | |
// using it: https://github.com/nicheinc/Website/blob/master/app/actions/user/init-user-action.js | |
// could use it: https://github.com/nicheinc/Website/blob/dev/app/utils/scroll-utils.js#L114-L129 | |
// https://github.com/nicheinc/Website/blob/feature/sherlock-refactor/app/utils/scroll-utils.js#L113-L126 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment