Created
April 27, 2009 16:29
-
-
Save robertsosinski/102585 to your computer and use it in GitHub Desktop.
Understanding how to bind scope in JavaScript with constructors
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
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