Why was JavaScript implemented using prototypal inheritance?

前端 未结 6 742
青春惊慌失措
青春惊慌失措 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 15:57

    I think it was chosen because it is easy to implement, needs no extra keywords and users don't need to understand it to be able to use the language. It is also more powerfull and flexible than class based inheritance.

    It's a natural choice for a untyped language. The main advantages of class based inheritance are that it allows static typing and thus type checking and a faster table based lookup implementation.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 16:08

    Unless one of the designers of JavaScript stops by to weigh in, we can only speculate. That having been said, here's my take:

    JavaScript is executed as it is interpreted, so there is no concept of separating the declaration of an object's type from the object itself. It's a very functional approach. The instance is coming into existence as it is being described - we are always operating on active instances. Because of this, the concept of a class - or a passive "instance template" - has no real place.

    0 讨论(0)
  • 2021-02-05 16:09

    Here's what Brendan Eich has to say about what happened: https://brendaneich.com/2008/04/popularity/

    As I've often said, and as others at Netscape can confirm, I was recruited to Netscape with the promise of "doing Scheme" in the browser. At least client engineering management including Tom Paquin, Michael Toy, and Rick Schell, along with some guy named Marc Andreessen, were convinced that Netscape should embed a programming language, in source form, in HTML.

    The diktat from upper engineering management was that the language must "look like Java". That ruled out Perl, Python, and Tcl, along with Scheme.

    I'm not proud, but I'm happy that I chose Scheme-ish first-class functions and Self-ish (albeit singular) prototypes as the main ingredients. The Java influences, especially y2k Date bugs but also the primitive vs. object distinction (e.g., string vs. String), were unfortunate.

    0 讨论(0)
  • 2021-02-05 16:14

    Because it was heavily influenced by Self. Both Wikipedia and the ECMA-spec mention this.

    0 讨论(0)
  • 2021-02-05 16:19

    Prototypical inheritance (with closures) allows others to do things that were never envisioned. It's the meshing of several paradigms that have come together to achieve general purpose programming.

    With a prototype language, you can have "mix-ins" for your classes. You can accomplish the level of encapsulation you desire, without language specific keywords. In short, prototype languages are awesome.

    I hate to say it, but JavaScript, plus some libraries, can do everything I need it to. It was subversive in its development (supposed to be subservient to Java). It has much power, in the simplest of implementations.

    With enough study / playing around, you'll begin to see the advantages of it's inspiration. JavaScript is one of the few languages that "hid" it's potential intentionally. You gotta get into the politics if you want to know the "why." But, it's for this reason, that it's awesome.

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