Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Last active August 29, 2015 14:03
Show Gist options
  • Save jczaplew/88c7330cd4b21307fced to your computer and use it in GitHub Desktop.
Save jczaplew/88c7330cd4b21307fced to your computer and use it in GitHub Desktop.
JS Scopes
var test = {
a: function() {
console.log("function a");
},
b: function(a) {
console.log(a*a);
},
c: function() {
(function() {
console.log("function c");
})();
},
// Logs "function a"
d: function() {
(function() {
(function() {
this.a();
}).bind(this)();
}).bind(this)();
},
// Logs 1, 4, 9
e: function() {
var thing = [1, 2, 3];
thing.forEach(function(d) {
this.b(d);
}.bind(this));
},
// Logs "function a" after 1 second
f: function() {
setTimeout(function() {
this.a();
}.bind(this), 1000);
},
g: function() {
this.z = "a string";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment