Created
December 12, 2014 09:46
-
-
Save timkg/69f9a068b6357de5717f to your computer and use it in GitHub Desktop.
// source http://jsbin.com/sabuv
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Closure - transporting a function outside of another function's scope. The returned function keeps reference to the inner scope, even when it is used somewhere else. | |
function makeGreeter (greeting) { | |
return function (name) { | |
console.log(greeting + ' ' + name); | |
} | |
} | |
var namaste = makeGreeter('Namaste'); | |
namaste('Joe'); | |
namaste('Sue'); | |
// Module Pattern - define public methods which can access methods hidden in scope by transporting public functions outside of enclosing function scope, for example using an IEFE | |
var math = (function () { | |
function privateMagic (num) { | |
return num + 0.0001; | |
} | |
var publicApi = { | |
sum: function (num1, num2) { | |
return privateMagic(num1 + num2); | |
} | |
} | |
return publicApi; | |
}()); | |
console.log(math.sum(1, 2)); | |
// "Modern Module Pattern" - wrap Module Pattern in meaningfully named function instead of IEFE, with module management and dependency injection | |
// see https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch5.md#modern-modules | |
/* | |
MyModules.define( "page", ["sequence"], function(sequence){ | |
var page = {}; | |
function start () {} | |
function finish () { | |
sequence.next() | |
} | |
return { | |
start: start, | |
finish: finish | |
}; | |
} ); | |
var page = MyModules.get('page'); | |
*/ | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Closure - transporting a function outside of another function's scope. The returned function keeps reference to the inner scope, even when it is used somewhere else. | |
function makeGreeter (greeting) { | |
return function (name) { | |
console.log(greeting + ' ' + name); | |
} | |
} | |
var namaste = makeGreeter('Namaste'); | |
namaste('Joe'); | |
namaste('Sue'); | |
// Module Pattern - define public methods which can access methods hidden in scope by transporting public functions outside of enclosing function scope, for example using an IEFE | |
var math = (function () { | |
function privateMagic (num) { | |
return num + 0.0001; | |
} | |
var publicApi = { | |
sum: function (num1, num2) { | |
return privateMagic(num1 + num2); | |
} | |
} | |
return publicApi; | |
}()); | |
console.log(math.sum(1, 2)); | |
// "Modern Module Pattern" - wrap Module Pattern in meaningfully named function instead of IEFE, with module management and dependency injection | |
// see https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch5.md#modern-modules | |
/* | |
MyModules.define( "page", ["sequence"], function(sequence){ | |
var page = {}; | |
function start () {} | |
function finish () { | |
sequence.next() | |
} | |
return { | |
start: start, | |
finish: finish | |
}; | |
} ); | |
var page = MyModules.get('page'); | |
*/</script></body> | |
</html> |
This file contains 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
// Closure - transporting a function outside of another function's scope. The returned function keeps reference to the inner scope, even when it is used somewhere else. | |
function makeGreeter (greeting) { | |
return function (name) { | |
console.log(greeting + ' ' + name); | |
} | |
} | |
var namaste = makeGreeter('Namaste'); | |
namaste('Joe'); | |
namaste('Sue'); | |
// Module Pattern - define public methods which can access methods hidden in scope by transporting public functions outside of enclosing function scope, for example using an IEFE | |
var math = (function () { | |
function privateMagic (num) { | |
return num + 0.0001; | |
} | |
var publicApi = { | |
sum: function (num1, num2) { | |
return privateMagic(num1 + num2); | |
} | |
} | |
return publicApi; | |
}()); | |
console.log(math.sum(1, 2)); | |
// "Modern Module Pattern" - wrap Module Pattern in meaningfully named function instead of IEFE, with module management and dependency injection | |
// see https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch5.md#modern-modules | |
/* | |
MyModules.define( "page", ["sequence"], function(sequence){ | |
var page = {}; | |
function start () {} | |
function finish () { | |
sequence.next() | |
} | |
return { | |
start: start, | |
finish: finish | |
}; | |
} ); | |
var page = MyModules.get('page'); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment