1

As I understand it: ES6 classes are basically constructor functions, right?

Of course with the benefit of not having to write stuff like this:

myFunction.prototype.myInheritablemethod= "someMethod";

Is this correct?

I'm asking because I'm aware there is the module pattern and the revealing module pattern, but those are not what ES6 classes are about, right?

1
  • 1
    ES6 classes are wrappers around Javascript's prototypal inheritance model, yes. May 12, 2016 at 7:40

1 Answer 1

4

Classes were introduced to provide syntactic sugar around the existing prototypal inheritance model of JavaScript. The following...

class Example {
  constructor(a, b) {
    this.a = a;
    this.b = b;
  }
  add() {
    return this.a + this.b;
  }
}

...is basically equivalent to this:

function Example(a, b) {
  this.a = a;
  this.b = b;
}
Example.prototype.add = function() {
  return this.a + this.b;
};

The class syntax along with the other bits it provides (like extends and static) makes it much easier and clearer to write code that deals with inheritance. But since it's just sugar around existing techniques it would probably make sense for you to properly understand pre-ES6 inheritance first.

1
  • So basically yes.. its equivalent to the constructor function...correct? May 12, 2016 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.