JavaScript .prototype如何工作? [英] How does JavaScript .prototype work?

查看:173
本文介绍了JavaScript .prototype如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是那种动态编程语言,但是我写了很多JavaScript代码。我从来没有真正了解这个基于原型的编程,有没有人知道这是如何工作的?

I'm not that into dynamic programming languages but I've written my fair share of JavaScript code. I never really got my head around this prototype-based programming, does any one know how this works?

var obj = new Object();
obj.prototype.test = function() { alert('Hello?'); };
var obj2 = new obj();
obj2.test();

我记得很久以前我和人们的讨论(我不确定我是什么)我正在做,但据我所知,没有一个阶级的概念。它只是一个对象,这些对象的实例是原始的克隆,对吗?

I remember a lot discussion I had with people a while back (I'm not exactly sure what I'm doing) but as I understand it, there's no concept of a class. It's just an object, and instances of those objects are clones of the original, right?

但是JavaScript中这个.prototype属性的确切目的是什么?它与实例化对象有什么关系?

But what is the exact purpose of this ".prototype" property in JavaScript? How does it relate to instantiating objects?

var obj = new Object(); // not a functional object
obj.prototype.test = function() { alert('Hello?'); }; // this is wrong!

function MyObject() {} // a first class functional object
MyObject.prototype.test = function() { alert('OK'); } // OK

这些幻灯片确实帮了很多。

推荐答案

每个JavaScript对象都有一个名为 [[Prototype]] 的内部属性。如果您通过 obj.propName obj ['propName'] 查找属性,并且该对象没有属性 - 可以通过 obj.hasOwnProperty('propName')进行检查 - 运行时查找[[Prototype]]引用的对象中的属性。如果prototype-object也没有这样的属性,则依次检查其原型,从而遍历原始对象的原型链,直到找到匹配或达到其结束。

Every JavaScript object has an internal property called [[Prototype]]. If you look up a property via obj.propName or obj['propName'] and the object does not have such a property - which can be checked via obj.hasOwnProperty('propName') - the runtime looks up the property in the object referenced by [[Prototype]] instead. If the prototype-object also doesn't have such a property, its prototype is checked in turn, thus walking the original object's prototype-chain until a match is found or its end is reached.

某些JavaScript实现允许直接访问[[Prototype]]属性,例如通过名为 __ proto __ 的非标准属性。通常,只能在对象创建期间设置对象的原型:如果通过 new Func()创建新对象,则对象的[[Prototype]]属性将为设置为 Func.prototype 引用的对象。

Some JavaScript implementations allow direct access to the [[Prototype]] property, eg via a non-standard property named __proto__. In general, it's only possible to set an object's prototype during object creation: If you create a new object via new Func(), the object's [[Prototype]] property will be set to the object referenced by Func.prototype.

这允许在JavaScript中模拟类,尽管JavaScript的继承系统是 - 正如我们所见 - 原型,而不是基于类:

This allows to simulate classes in JavaScript, although JavaScript's inheritance system is - as we have seen - prototypical, and not class-based:

只需将构造函数视为类和原型的属性(即引用的对象)构造函数的 prototype 属性)作为共享成员,即每个实例的成员相同。在基于类的系统中,方法以相同的方式为每个实例实现,因此方法通常被添加到原型中,而对象的字段是特定于实例的,因此在构造期间添加到对象本身。

Just think of constructor functions as classes and the properties of the prototype (ie of the object referenced by the constructor function's prototype property) as shared members, ie members which are the same for each instance. In class-based systems, methods are implemented the same way for each instance, so methods are normally added to the prototype, whereas an object's fields are instance-specific and therefore added to the object itself during construction.

这篇关于JavaScript .prototype如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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