Last active
March 8, 2018 22:55
-
-
Save jonasraoni/c02eb36539bcefdda03372771fbaf108 to your computer and use it in GitHub Desktop.
A variable that increments itself whenever it's accessed.
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
class Increment{ | |
constructor(value) { | |
this.current = +value || 0; | |
}; | |
[Symbol.toPrimitive]() { | |
return ++this.current; | |
} | |
} | |
var increment = new Increment(); | |
alert(increment); | |
alert(increment); | |
alert(increment + increment); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment