👈 back to home

Vinit Kumar

Some Notes On The Classes in JS

December 07, 2013

Here is an interesting example for Classes in JS. Classes in JS don’t have a class keyword.

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(4,20);
r.includes(5);
r.foreach(console.log);
console.log(r);

It is pretty simple and clean way to create a class


Hello Hello! I am Vinit Kumar. I am a software engineer who loves making computers do things and admires good books, music, art and coffee. You can read more about me. Find my resume, values and code here.

© 2020, Vinit Kumar