Skip to content

Instantly share code, notes, and snippets.

@soharu
Last active January 2, 2016 09:39
Show Gist options
  • Save soharu/8284255 to your computer and use it in GitHub Desktop.
Save soharu/8284255 to your computer and use it in GitHub Desktop.
Constructor function example
function Range(from, to) {
this.from = from;
this.to = to;
}
Range.prototype = {
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