Last active
August 29, 2015 14:07
-
-
Save willmcneilly/4ab7bec6183f7bd554d0 to your computer and use it in GitHub Desktop.
Overriding valueOf in custom objects
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
function Counter(){ | |
this.count = 0; | |
} | |
Counter.prototype.incr = function() { | |
this.count++; | |
} | |
Counter.prototype.valueOf = function() { | |
return this.count; | |
} | |
c = new Counter(); | |
c.incr(); // this.count now === 1; | |
console.log(c.count); // will return 1; | |
console.log(c + 2); // will return 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment