Why was JavaScript implemented using prototypal inheritance?

前端 未结 6 858
青春惊慌失措
青春惊慌失措 2021-02-05 15:44

There are lots of articles and posts explaining how JavaScript inheritance works, but why was JavaScript implemented using prototypal inheritance instead of classical inheritanc

6条回答
  •  没有蜡笔的小新
    2021-02-05 16:06

    JavaScript was originally supposed to be very much like Lisp. Even after the syntax was changed to more closely resemble C/Java, it is still Lisp in C's clothing. I think the answer lies in it's functional programming origins. In pure FP, there is no mutable state, which means no mutable objects. If you relax the rules a bit and get slightly creative, you end up with something like protypal inheritance, i.e., you can extend objects but not modify the original object. It provides the same power as inheritance and still gives you some immutability.

    Finally, twist the language around to make it look like C++ and Java, and viola, you have new someFunction() and the rest is history.

提交回复
热议问题