简体   繁体   English

Function.prototype.__proto__ 是什么意思?

[英]What is meaning of Function.prototype.__proto__?

I am a beginner in Javascript and am learning about Object Oriented and prototyping in it.我是Javascript的初学者,正在学习Object Orientedprototyping

As far as I know, Object is a function and is created by Function because Object.__proto__ === Function.prototype but looking through various diagrams online, I am quite confused by fact that How Function.prototype.__proto__ === Object.prototype .据我所知, Object是一个函数,由 Function 创建,因为Object.__proto__ === Function.prototype但是通过网上的各种图表,我对 How Function.prototype.__proto__ === Object.prototype

What does Function.prototype.__proto__ mean? Function.prototype.__proto__是什么意思?

Isn't it something that is developed by language owners as Function is the first thing from which everything arrives.它不是由语言所有者开发的东西,因为函数是一切到达的第一件事。

Then what does it mean?那么它是什么意思呢? Am I lacking some important fact?我是否缺少一些重要的事实? I looked through other StackOverflow answers but can't find anything related to it.我查看了其他StackOverflow答案,但找不到任何与之相关的内容。

myFunc.prototype is the __proto__ of any object constructed by calling new myFunc() . myFunc.prototype是通过调用new myFunc()构造的任何对象的__proto__

Thinking in terms of classical (Java- or C++-style) OO, you could say that myFunc is a constructor and (therefore) myFunc.prototype is the class.从经典(Java 或 C++ 风格)OO 的角度考虑,您可以说myFunc是一个构造函数,(因此) myFunc.prototype是类。 In that sense, myFunc.prototype.__proto__ is the superclass;从这个意义上说, myFunc.prototype.__proto__是超类; that is, the prototype of the prototype of all objects created with new myFunc .即所有用new myFunc创建的对象的原型的原型。

One useful thing you can do to myFunc.prototype.__proto__ is assign to it to create a superclass relationship, eg您可以对myFunc.prototype.__proto__做的一件有用的事情是分配给它以创建超类关系,例如

myFunc.prototype.__proto__ = mySuperclassConstructor.prototype

This idiom sheds light on why Function.prototype.__proto__ === Object.prototype holds (the core of your question): it simply means that Function is a subclass of Object — Or in other words, JavaScript runtimes do something equivalent to the above code snippet in their prelude so as to make Function a subclass of Object (as they should, per ECMA-262 §§ 19.2.2 and 19.2.3)这个习语Function.prototype.__proto__ === Object.prototype了为什么Function.prototype.__proto__ === Object.prototype成立(你的问题的核心):它只是意味着FunctionObject一个子类——或者换句话说,JavaScript 运行时做一些与上面等效的事情前奏中的代码片段,以便使Function成为Object的子类(按照ECMA-262 §§ 19.2.2 和 19.2.3,他们应该这样做)

Careful though, while __proto__ happens to work on all modern (2019) JavaScript implementations (node.js and browsers), its use is both non-standard and slow .不过小心,虽然__proto__ 恰好适用于所有现代(2019)JavaScript 实现(node.js 和浏览器),但它的使用既非标准又缓慢 Consider using “real” ES6 classes instead. 考虑使用“真正的” ES6 类。

TL;DR TL; 博士

__proto__ is a property of an object which enables you to look up in the prototype chain. __proto__是一个object的属性,它使您可以在原型链中查找。 While prototype is a property of a Function which enables you to add shareable functionalities to a constructor function.虽然prototypeFunction一个属性,它使您能够向构造函数添加可共享的功能。

Long Answer长答案

Understand this from an example, let's say you create a constructor function.从示例中理解这一点,假设您创建了一个构造函数。

function A() {} and then create an instance of it, var a = new A() . function A() {}然后创建它的一个实例var a = new A()

Then, add a function as following:然后,添加一个函数如下:

A.prototype.getA = function () { return 'A'; } A.prototype.getA = function () { return 'A'; } . A.prototype.getA = function () { return 'A'; } .

Now, if you try to access a.getA() , you'll get the result, ie getA will be executed.现在,如果您尝试访问a.getA() ,您将得到结果,即getA将被执行。

But how does it know about the function getA even though getA has been added after the instance a was created.但是,即使在创建实例a之后添加了getA ,它如何知道函数getA It's because of using __proto__ , you can traverse up in the chain (you must have heard about prototype chaining).正是因为使用了__proto__ ,才能在链中向上遍历(你一定听说过原型链)。

Technically, __proto__ is a property of an object, while prototype is a property of function .从技术上讲, __proto__是对象的属性,而prototypefunction的属性。 But how could functions have property?但是functions怎么会有属性呢? Because everything in JavaScript is converted implicitly to an object.因为 JavaScript 中的所有内容都隐式转换为对象。 Ever wondered how could you something like this: 'test'.toUpperCase() ?有没有想过你怎么会像这样: 'test'.toUpperCase() Isn't string literals are 'not object' and they are primitives?字符串文字不是“非对象”,它们是基元吗?

Read this for reference: http://jayendra.co.in/objects-in-javascript/阅读本文以供参考: http : //jayendra.co.in/objects-in-javascript/

Now to answer your question:现在回答你的问题:

What does Function.prototype.__proto__ mean? Function.prototype.__proto__是什么意思?

You are trying to access the prototype property of Function constructor function.您正在尝试访问Function构造函数的prototype属性。 Remember, prototype itself is an object , so you can access properties like constructor and __proto__ .请记住, prototype本身是一个object ,因此您可以访问诸如constructor__proto__类的属性。

Function.prototype.__proto__ === Object.prototype

To enable chaining, when you access __proto__ property, you are looking up!要启用链接,当您访问__proto__属性时,您正在查找!

Any function can access all the properties of an object.任何函数都可以访问对象的所有属性。 How?如何?

A property of Object , let's say toString . Object一个属性,比方说toString You can do, A.toString() // A created at the start .你可以这样做, A.toString() // A created at the start But we never created a function toString for A constructor function!但是我们从来没有为A构造函数创建一个toString函数!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM