Created
July 25, 2013 20:26
-
-
Save gkatsev/6083430 to your computer and use it in GitHub Desktop.
A toggleable object.
This file contains 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
var Toggleable = function(a, b) { | |
this._a = a; | |
this._b = b; | |
}; | |
Toggleable.prototype.valueOf = function() { return this._a; }; | |
Toggleable.prototype.toggle = function() { | |
if (this.valueOf() == this._a) { | |
this.valueOf = function() { return this._b; }; | |
} else { | |
this.valueOf = function() { return this._a; }; | |
} | |
}; | |
var foo = new Toggleable(1, 2); | |
foo + foo; | |
// 2 | |
foo.toggle(); | |
// 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment