Nurbol Alpysbayev
Nurbol Alpysbayev

Reputation: 21891

What can prototypal approach do that es6 class can't?

Is there anything that can only be achieved with classic/old prototypal approach, yet?

Why I need this: imagine I am writing a subset of JS that is free of old stuff (prototypes instead of classes, var, etc.)

Upvotes: 0

Views: 44

Answers (1)

trincot
trincot

Reputation: 350310

There are several things, including but not limited to:

  • When using the function notation, you can call the constructor without new. The class notation forbids this.

    As a side note: where sub-classing would tradionally be done with something like MyParentClass.call(this) (which is not allowed when MyParentClass was defined with class), you would now combine the class syntax with the extends and super keywords.

  • function declarations can appear after their use, since they are hoisted, class declarations are not.
  • function declarations can be overridden by a new declaration, class declarations cannot redefine an earlier declaration
  • Prototype methods can be used as constructors when defined in the old style, not when defined using the class syntax.
  • function declarations can be made in "sloppy" mode (not strict), while class methods always run in strict mode. As a consequence you cannot use the caller, callee or arguments properties of the methods/constructor in class notation. All other consequences of strict mode apply as well of course. It would go too far too list them all here.
  • You can assign a new object to an old-style function's prototype property (it is writable). With class the prototype property is not writable (note that this does not mean you cannot mutate it).

Upvotes: 2

Related Questions