Why the property “prototype” is absent in definition methods of ES6 classes

余生颓废 提交于 2019-12-04 18:02:24

But I can't understand WHY?

ES2015 distinguishes between two types of functions:

  • callable functions: functions that can be called without new, i.e. foo().
  • constructable functions: functions that can be called with new.

Whether a function is callable or constructable or both depends on how it is defined. The specification simply dictates that functions declared via the method syntax are not constructable.

Now, that doesn't explain the reasons behind this decision. I can't speak for the TC39 committee, but a clear effort with ES2015 was to reduce some of the surprising behaviors around functions. As such it was enforced how certain types of functions can be used. A method is conceptually not a constructor and thus you cannot call it as such.


Constructable functions:

  • Classes

Callable functions:

  • Arrow functions
  • Object/class methods (via method syntax)
  • Generator functions
  • Async functions

Both:

  • Function declarations/expressions
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!