Created
June 8, 2012 20:08
-
-
Save silentrob/2897913 to your computer and use it in GitHub Desktop.
Broken code.
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
FMWK.button = function() { | |
var _this = this; | |
this.el = document.createElement('button'); | |
['hide','show'].forEach(function(method){ | |
FMWK.button.prototype[method] = function(){ | |
$(_this.el)[method].apply($(_this.el), Array.prototype.slice.call(arguments)); | |
} | |
}); | |
return this; | |
} | |
var b1 = new FMWK.button(); | |
var b2 = new FMWK.button(); | |
b1.hide() // fires on b2 |
Every time you construct a new instance of button, you are overwriting the button prototype with new show and hide methods and hard coding _this to those methods. Set the show/hide methods outside of the constructor and reference this.el directly in there.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is _this.el is not closed over correctly, however wrapping it in a function and passing it in looses the arguments from the outer scope.