Skip to content

Instantly share code, notes, and snippets.

@robertsosinski
Created April 27, 2009 16:29
Show Gist options
  • Save robertsosinski/102585 to your computer and use it in GitHub Desktop.
Save robertsosinski/102585 to your computer and use it in GitHub Desktop.
Understanding how to bind scope in JavaScript with constructors
Function.prototype.bind = function(scope) {
_function = this;
return function() {
_function.apply(scope, arguments);
}
}
window.scope = "window";
Object = function() {
this.scope = "object";
this.run = function() {
var action = function(word) {
console.log(word + " " + this.scope);
}
action.bind(this)("hello");
}
}
obj = new Object();
obj.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment