Skip to content

Instantly share code, notes, and snippets.

@soharu
Created January 6, 2014 16:11
Show Gist options
  • Save soharu/8285069 to your computer and use it in GitHub Desktop.
Save soharu/8285069 to your computer and use it in GitHub Desktop.
Example of constructor property 3
function Range(from, to) {
this.from = from;
this.to = to;
}
Range.prototype = {
constructor: Range,
includes: function (x) {
return this.from <= x && x <= this.to;
},
foreach: function (f) {
for (var x = Math.ceil(this.from); x <= this.to; x++)
f(x);
},
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