Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

javascript - Javascript是基于原型的语言是什么意思?(What does it mean that Javascript is a prototype based language?)

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基于原型是什么意思,为什么这是一个优势呢?)

  ask by Jonas Pegerfalk translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Prototypal inheritance is a form of object-oriented code reuse .

(原型继承是面向对象的代码重用的一种形式。)

Javascript is one of the only [mainstream] object-oriented languages to use prototypal inheritance.

(Javascript是使用原型继承的唯一[主流]面向对象语言之一。)

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.

(这意味着对象与同一类的其他对象以及其父类共享代码。)

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.

(在javascript中,每个对象都有一个秘密链接到创建它的对象,形成一个链。)

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.

(当一个对象被要求输入它没有的属性时,它的父对象将被询问......不断向上链,直到找到该属性或者直到达到根对象为止。)

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.

(JavaScript中的每个函数(它们都是对象本身)实际上有一个名为“prototype”的成员,它负责在要求对象时提供值。)

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.

(将属性添加到函数对象的原型将使其可用于构造的对象,以及从其继承的所有对象。)

Advantages

(好处)

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.

(但是古典语言当然也能够实现这一目标。)

Sidenote: @Andrew Hedges makes a good point, that there are actually many prototypal languages.

(旁注: @Andrew Hedges提出了一个很好的观点,即实际上有许多原型语言。)

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.

(NewtonScript似乎有一段时间的牵引力,但死于其??平台。)

It's also possible to extend some modern languages in ways which add prototypal capabilities.

(也可以通过添加原型功能的方式扩展一些现代语言。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...