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

查看:28
本文介绍了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天全站免登陆