Javascript是基于原型的语言是什么意思? [英] What does it mean that Javascript is a prototype based language?

查看:159
本文介绍了Javascript是基于原型的语言是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说Javascript的一个主要优点是它是基于原型的语言。

One of the major advantages with Javascript is said to be that it is a prototype based language.

但Javascript基于原型是什么意思,为什么这是一个优势呢?

But what does it mean that Javascript is prototype based, and why is that an advantage?

推荐答案

原型继承是面向对象的代码重用的一种形式。 Javascript是使用原型继承的唯一[主流]面向对象语言之一。几乎所有其他面向对象的语言都是经典的。

Prototypal inheritance is a form of object-oriented code reuse. Javascript is one of the only [mainstream] object-oriented languages to use prototypal inheritance. Almost all other object-oriented languages are classical.

经典继承中,程序员编写了一个定义对象的类。可以从同一个类中实例化多个对象,因此您可以在一个位置使用代码来描述程序中的多个对象。然后可以将类组织成层次结构,从而进一步重用代码。更通用的代码存储在更高级别的类中,较低级别的类继承该类。这意味着一个对象与同一个类的其他对象以及它的父类共享代码。

In classical inheritance, the programmer writes a class, which defines an object. Multiple objects can be instantiated from the same class, so you have code in one place which describes several objects in your program. Classes can then be organized into a hierarchy, furthering code reuse. More general code is stored in a higher-level class, from which lower level classes inherit. This means that an object is sharing code with other objects of the same class, as well as with its parent classes.

原型继承中表单,对象直接从其他对象继承。关于课程的所有业务都消失了。如果你想要一个对象,你只需要写一个对象。但代码重用仍然是一件有价值的事情,因此允许对象在层次结构中链接在一起。在javascript中,每个对象都有一个到创建它的对象的秘密链接,形成一个链。当一个对象被要求提供它没有的属性时,它的父对象将被询问......不断向上链,直到找到该属性或者直到达到根对象为止。

In the prototypal inheritance form, objects inherit directly from other objects. All of the business about classes goes away. If you want an object, you just write an object. But code reuse is still a valuable thing, so objects are allowed to be linked together in a hierarchy. In javascript, every object has a secret link to the object which created it, forming a chain. When an object is asked for a property that it does not have, its parent object will be asked... continually up the chain until the property is found or until the root object is reached.

JavaScript中的每个函数(对象本身)实际上都有一个名为prototype的成员,它负责在要求对象时提供值。拥有此成员允许构造函数机制(通过其从对象构造函数)工作。将属性添加到函数对象的原型中将使其可用于构造的对象,以及从其继承的所有对象。

Each function in JavaScript (which are objects themselves) actually has a member called "prototype", which is responsible for providing values when an object is asked for them. Having this member allows the constructor mechanism (by which objects are constructed from functions) to work. Adding a property to the prototype of a function object will make it available to the constructed object, as well as to all of the objects which inherit from it.

优点

为什么原型继承是代码重用的有利形式,可能没有一个硬性规则。代码重用本身是有利的,原型继承是一种合理的方式来实现它。您可能会认为原型继承是代码重用的一个相当简单模型,并且该代码可以在直接方式中大量重用。但是古典语言肯定能够实现这一点。

There may not be a hard and fast rule as to why prototypal inheritance is an advantageous form of code-reuse. Code reuse itself is advantageous, and prototypal inheritance is a sensible way of going about it. You might argue that prototypal inheritance is a fairly simple model of code reuse, and that code can be heavily reused in direct ways. But classical languages are certainly able to accomplish this as well.

旁注: @Andrew Hedges 提出了一个很好的观点,实际上有许多原型语言。值得注意的是,这些其他存在,但也值得注意的是,它们都没有接近主流。 NewtonScript似乎有一段时间的牵引力,但死于其​​平台。也可以通过增加原型功能的方式扩展一些现代语言。

Sidenote: @Andrew Hedges makes a good point, that there are actually many prototypal languages. It's worth noting that these others exist, but also worth noting that none of them are anything close to mainstream. NewtonScript seemed to have some traction for a while, but died with its platform. It's also possible to extend some modern languages in ways which add prototypal capabilities.

这篇关于Javascript是基于原型的语言是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆