__proto__ VS. prototype in JavaScript
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了__proto__ VS. prototype in JavaScript,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1902字,纯文字阅读大概需要3分钟。
内容图文
![__proto__ VS. prototype in JavaScript](/upload/InfoBanner/zyjiaocheng/777/c929217b44f9482da5c31128141663a1.jpg)
__proto__ VS. prototype in JavaScript
http://dmitrysoshnikov.com/ecmascript/javascript-the-core/#a-prototype-chain
// a constructor function function Foo(y) { // which may create objects // by specified pattern: they have after // creation own "y" property this.y = y; }
__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;
How does __proto__ differ from constructor.prototype?
I've been trying to wrap my head around this recently and finally came up with this "map" that I think sheds full light over the matter
I know I'm not the first one making this up but it was more interesting figuring it out that finding it :-). Anyway, after that I found e.g. this another diagram that I think says basicly the same:
The most surprising thing for me was discovering that Object.__proto__
points to Function.prototype
, instead of Object.prototype
, but I'm sure there's a good reason for that :-)
I paste the code mentioned in the image here as well for if anyone wants to test it. Note that some properties are added to the objects for making easy to know where we are after some jumps:
Object.O1=''; Object.prototype.Op1=''; Function.F1 = ''; Function.prototype.Fp1 = ''; Cat = function(){}; Cat.C1 = ''; Cat.prototype.Cp1 = ''; mycat = new Cat(); o = {}; // EDITED: using console.dir now instead of console.log console.dir(mycat); console.dir(o);
The reason why Object.__proto__
points to Function.prototype
is because Object()
by itself is a native function that instantiates an empty object. Therefore, Object()
is a function. You'll find that all the other major native types' __proto__
properties point to Function.prototype
. Object
, Function
, String
, Number
, and Array
all inherit the Function prototype.
内容总结
以上是互联网集市为您收集整理的__proto__ VS. prototype in JavaScript全部内容,希望文章能够帮你解决__proto__ VS. prototype in JavaScript所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。