Created
March 25, 2010 01:30
-
-
Save coolaj86/343056 to your computer and use it in GitHub Desktop.
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
// Protect the global namespace | |
(function(){ | |
// Create a static scope | |
var a = 1, | |
b = 2, | |
c = 3; | |
d = 4; | |
// Create a fun little object to toy with | |
var Obj = new function(){ | |
var b = 5; // this private 'b' takes precedence its namesake in the closure | |
this.c = 6; // this public 'c' doesn't obstruct the 'c' above | |
var doStuff = function(){ console.log('doing stuff'); }; | |
this.doMoreStuff = function(){ console.log('doing more stuff'); }; | |
function doEvenMoreStuff(){ console.log('doing even more stuff!!!'); }; | |
this.getErDone = function(){ console.log("Get 'er Done!"); return doEvenMoreStuff(); }; | |
var d = d; // this would probably be undefined due to 'hoisting' | |
var e = a; // e is set to 1 | |
}; | |
window.Obj = Obj; | |
}()); | |
// A more differenter scope | |
(function(){ | |
str = JSON.stringify; | |
var funky = function(d){ | |
var i; | |
//arr = [a,b,c,this.c,d]; | |
arr = [this.c,d]; | |
for (i in arr) { | |
console.log(str(arr[i])); | |
} | |
//doStuff(); | |
this.doMoreStuff(); | |
//this.doEvenMoreStuff(); | |
//doEvenMoreStuff(); | |
this.getErDone(); | |
}; | |
window.funky = funky; | |
}()); | |
// What will the output look like? | |
funky.call(Obj,100); | |
/* | |
//Firefox Firebug says: | |
>>> 6 | |
>>> 100 | |
>>> doing more stuff | |
>>> Get 'er Done! | |
>>> doing even more stuff!!! | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment