__proto__ VS.JavaScript 中的原型 [英] __proto__ VS. prototype in JavaScript

查看:31
本文介绍了__proto__ VS.JavaScript 中的原型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个图再次表明每个对象都有一个原型.构造函数函数 Foo 也有自己的 __proto__ 即 Function.prototype,并且反过来也通过它的 __proto__ 属性再次引用到Object.prototype.因此,重复一遍, Foo.prototype 只是一个显式的Foo 的属性,指的是 b 和 c 对象的原型.

This figure again shows that every object has a prototype. Constructor function Foo also has its own __proto__ which is Function.prototype, and which in turn also references via its __proto__ property again to the Object.prototype. Thus, repeat, Foo.prototype is just an explicit property of Foo which refers to the prototype of b and c objects.

var b = new Foo(20);
var c = new Foo(30);

__proto__prototype 有什么区别?

该图取自 dmitrysoshnikov.com.

注意:现在有 第二版(2017 年) 到上述 2010 年的文章.

Note: there is now a 2nd edition (2017) to the above 2010 article.

推荐答案

__proto__ 是查找链中用于解析方法等的实际对象.prototype是使用 new 创建对象时用于构建 __proto__ 的对象:

__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new:

( new Foo ).__proto__ === Foo.prototype;
( new Foo ).prototype === undefined;

这篇关于__proto__ VS.JavaScript 中的原型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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