Skip to content

Instantly share code, notes, and snippets.

@gkatsev
Created July 25, 2013 20:26
Show Gist options
  • Save gkatsev/6083430 to your computer and use it in GitHub Desktop.
Save gkatsev/6083430 to your computer and use it in GitHub Desktop.
A toggleable object.
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