Skip to content

Instantly share code, notes, and snippets.

@jonasraoni
Last active March 8, 2018 22:55
Show Gist options
  • Save jonasraoni/c02eb36539bcefdda03372771fbaf108 to your computer and use it in GitHub Desktop.
Save jonasraoni/c02eb36539bcefdda03372771fbaf108 to your computer and use it in GitHub Desktop.
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