为什么 JavaScript 使用原型继承来实现?

发布于 2024-08-18 16:22:23 字数 119 浏览 10 评论 0原文

有很多文章和帖子解释了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤君无依 2024-08-25 16:22:23

以下是布伦丹·艾奇 (Brendan Eich) 对所发生事件的看法:
https://brendaneich.com/2008/04/popularity/

正如我经常说的,并且正如 Netscape 的其他人可以证实的那样,我被招募到 Netscape 时,承诺要在浏览器中“执行计划”。至少包括 Tom Paquin、Michael Toy 和 Rick Schell 在内的客户工程管理人员,以及一些名叫 Marc Andreessen 的人,都相信 Netscape 应该以源代码形式在 HTML 中嵌入一种编程语言。

上层工程管理人员的命令是该语言必须“看起来像 Java”。这就排除了 Perl、Python 和 Tcl 以及Scheme。

我并不自豪,但我很高兴我选择了计划式的一流函数和自私的(尽管是单一的)原型作为主要成分。 Java 的影响,尤其是 y2k Date 错误,以及原始与对象的区别(例如,字符串与字符串),都是不幸的。

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.

童话里做英雄 2024-08-25 16:22:23

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.

安稳善良 2024-08-25 16:22:23

因为它受到自我的影响很大。维基百科和 ECMA 规范都提到了这一点。

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

讽刺将军 2024-08-25 16:22:23

我认为选择它是因为它易于实现,不需要额外的关键字,并且用户不需要理解它就可以使用该语言。它也比基于类的继承更强大、更灵活。

对于非类型化语言来说,这是一个自然的选择。基于类的继承的主要优点是它允许静态类型,从而允许类型检查和更快的基于表的查找实现。

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.

新人笑 2024-08-25 16:22:23

原型继承(带有闭包)允许其他人做从未设想过的事情。它是几种范式的结合,这些范式结合在一起以实现通用编程。

使用原型语言,您可以为您的类添加“混合”。您可以实现所需的封装级别,而无需特定于语言的关键字。简而言之,原型语言非常棒。

我不想这么说,但 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文