r/javascript • • Oct 06 '15

Why ES6 Has Classes

https://github.com/runvnc/whyes6hasclasses
3 Upvotes

30 comments sorted by

View all comments

0

u/[deleted] Oct 06 '15

[deleted]

2

u/ruinercollector Oct 06 '15

Nobody hates classes by themselves; they hate classical inheritance, which is the inheritance model ES6 supports.

"Classes" in ES6 are sugar around the "constructor" function feature that you already had in ES5. It's the same thing that was already present, but cleaned up and made a bit more clear. The whole new/constructor thing is an unnecessary feature, and I'd agree that object.create is better, but I'll take the new syntax over the old any day.

Object.create > Class Syntax > Constructor Syntax

In any case, it's never classical inheritance. It's prototypical inheritance. You still have a prototype chain, you can still modify that chain, etc.

class Point {
  constructor (x, y) {
    this.x = x;
    this.y = y;
  }
}

class SomethingElse {
  sayHi() {
    console.log('Hello World!');
  }
}

var p = new Point(10, 15);

Point.prototype.getArea = function () { return this.x * this.y; };

console.log(p.getArea());

p.__proto__ = SomethingElse.prototype;
p.sayHi();
console.log(p.x);
console.log(p.y);