Difference between constructor in ES6 class and constructor in prototype?

后端 未结 1 976
小蘑菇
小蘑菇 2021-01-25 19:28

Both ES6 class and prototype of function have a contructor, but I\'m wondering are they the same? Let me give more explanations.

So, I create a Cat function

相关标签:
1条回答
  • 2021-01-25 19:57

    Since classes are just a syntactic sugar over prototype inheritance, is the constructor function in Dog class just the same as in Cat function?

    Yes, the constructor-prototype relationship still works the same.

    There are a few differences though, for example Dog.prototype is not writable and Dog can only be called with new.

    0 讨论(0)
提交回复
热议问题