K.David
K.David

Reputation: 13

change javascript object's method on the fly

I have a javascript object called Button. This is an object on a HTML5 canvas, and changes it's color when the mouse enters it:

Button.prototype.handleMouseMove = function() {
    if(this.isMouseOver() && !this.mouseIsOver) {this.onMouseIn(); this.mouseIsOver = true;}
    if(!this.isMouseOver() && this.mouseIsOver) {this.onMouseOut(); this.mouseIsOver = false;}
}

Button.prototype.onMouseIn = function() {
    this.setColor("red");
}

I'd like to change onMouseIn method on the fly,but I don't really know how to do it.

I've tried:

button1.onMouseIn = button1.setColor("xy");

and

Button.prototype.changeMouseInAction(fn) {
    this.onMouseIn = fn;
}
button1.changeMouseInAction(button1.setColor("xy"));

but nothing works.

Upvotes: 1

Views: 4660

Answers (1)

Thomas Harris
Thomas Harris

Reputation: 434

Try this :-

  button1.onMouseIn = function(){
                         button1.setColor("xy");
                      }

Upvotes: 2

Related Questions