为什么 JavaScript 使用原型继承来实现?
有很多文章和帖子解释了 JavaScript 继承的工作原理,但为什么 JavaScript 使用原型继承而不是经典继承来实现呢?
我喜欢 JavaScript,所以我并不是说它是坏事......我只是好奇。
There are lots of articles and posts explaining how JavaScript inheritance works, but why was JavaScript implemented using prototypal inheritance instead of classical inheritance?
I love JavaScript, so I'm not saying it's bad thing... I'm just curious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是布伦丹·艾奇 (Brendan Eich) 对所发生事件的看法:
https://brendaneich.com/2008/04/popularity/
Here's what Brendan Eich has to say about what happened:
https://brendaneich.com/2008/04/popularity/
JavaScript 最初被认为与 Lisp 非常相似。即使在语法更改为更类似于 C/Java 之后,它仍然是披着 C 外衣的 Lisp。我认为答案在于它的函数式编程起源。在纯 FP 中,没有可变状态,这意味着没有可变对象。如果您稍微放松规则并稍微发挥创意,您最终会得到类似于原型继承的东西,即您可以扩展对象但不能修改原始对象。它提供了与继承相同的功能,并且仍然为您提供了一些不变性。
最后,扭转一下语言,使其看起来像 C++ 和 Java,中提琴,你有
new someFunction()
,其余的都是历史了。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.因为它受到自我的影响很大。维基百科和 ECMA 规范都提到了这一点。
Because it was heavily influenced by Self. Both Wikipedia and the ECMA-spec mention this.
我认为选择它是因为它易于实现,不需要额外的关键字,并且用户不需要理解它就可以使用该语言。它也比基于类的继承更强大、更灵活。
对于非类型化语言来说,这是一个自然的选择。基于类的继承的主要优点是它允许静态类型,从而允许类型检查和更快的基于表的查找实现。
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.
原型继承(带有闭包)允许其他人做从未设想过的事情。它是几种范式的结合,这些范式结合在一起以实现通用编程。
使用原型语言,您可以为您的类添加“混合”。您可以实现所需的封装级别,而无需特定于语言的关键字。简而言之,原型语言非常棒。
我不想这么说,但 JavaScript 加上一些库,可以做我需要它做的一切。它的发展具有颠覆性(本来应该服从于Java)。在最简单的实现中,它具有强大的功能。
经过足够的研究/尝试,您将开始看到它的灵感的优势。 JavaScript 是少数有意“隐藏”其潜力的语言之一。如果你想知道“为什么”,你就必须涉足政治。但是,正是因为这个原因,它才很棒。
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.