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

后端 未结 4 1475
长发绾君心
长发绾君心 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:00

    I don't think I agree with your premise, but the major benefit of prototype based inheritance is that it allows runtime assignment of properties and methods on all members of the class, even when there are already instances of that class.

    The theorist in me actually cringes at some of those implications. Imagine debugging something where you have no idea which piece of code has completely re-defined your class structure. It is beyond daunting.

    But, I will say, that it has proven useful:

    Once, when I was working in ActionScript 2 (not for the faint of heart as it is almost a non-deterministic language) inside of an ActionScript 3 container. An unknown side-effect of this problem is that it eliminates the idea of memory levels (something essential to my company's legacy code). I was able to add the line: MovieClip.prototype._level0 = _root; and it solved the problem.

    To counter my above argument, the fact that you can modify all instances after the fact does provide you with a certain amount of power in situations where you do not have access to the original code base (like the example), but I see no major benefit beyond that.

提交回复
热议问题