Skip to content

Instantly share code, notes, and snippets.

@jonasraoni
Last active Mar 8, 2018
Embed
What would you like to do?
A variable that increments itself whenever it's accessed.
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