Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Broken code.
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
@silentrob

This comment has been minimized.

Copy link
Owner Author

silentrob commented Jun 8, 2012

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.

@filmaj

This comment has been minimized.

Copy link

filmaj commented Jun 8, 2012

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
You can’t perform that action at this time.