Skip to content

Instantly share code, notes, and snippets.

@jonasraoni
Last active March 8, 2018 22:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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