Skip to content

Instantly share code, notes, and snippets.

@soharu
Created January 6, 2014 16:09
Show Gist options
  • Save soharu/8285049 to your computer and use it in GitHub Desktop.
Save soharu/8285049 to your computer and use it in GitHub Desktop.
Example of constructor property 2
function Range(from, to) {
this.from = from;
this.to = to;
}
Range.prototype.includes = function (x) {
return this.from <= x && x <= this.to;
};
Range.prototype.foreach = function (f) {
for (var x = Math.ceil(this.from); x <= this.to; x++)
f(x);
};
Range.prototype.toString = function () {
return "(" + this.from + "..." + this.to + ")";
};
var r = new Range(1, 3);
r.includes(2);
r.foreach(console.log);
console.log(r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment