What are patterns you could use with prototype inheritance that you cannot with class?

后端 未结 4 1472
长发绾君心
长发绾君心 2021-02-05 12:37

Everyone seems to generally agree that prototype inheritance is simpler and more flexible than class inheritance. What I have not seen in the literature that I\'ve read is very

4条回答
  •  孤街浪徒
    2021-02-05 13:08

    Since there is no real class inheritance in JavaScript, you will probably never find any literature explaining what you can't do with classic class inheritance in JavaScript, unless you compare it with class inheritance in other languages.

    So I'll consider your question as if you meant:

    What are some patterns that you can use with JavaScript prototype inheritance that you cannot do with class inheritance in languages that support it?

    R= In general, most object oriented languages that are based in Class Inheritance produce objects that have a rigid structure, they will always have the same methods and properties during their life and all the objects of the same classes will have the same structure.

    So, in general, the patterns you can apply with a prototype based language that can't be implemented with a class inheritance language are the ones that depend on:

    • Add or remove methods or properties to objects, on the fly

    *This is so well supported in JavaScript due to it's dynamic an cross domain collaborative nature. Sometimes you need to load external scripts from other websites but need to add new functionalities without being able to change the source-code from these external libraries.

提交回复
热议问题