本文介绍了函数对象__proto__和原型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚我编写的函数的原型链

I am trying to figure out the prototype chain of a function that I wrote

function Animal(voice)
 {
  this.voice = voice|| 'meaw'
  }

  Animal.prototype.speak =function() {
  console.log(this.voice);
  }

我知道Animal函数具有原型属性,该属性指向它的原型对象.它的原型对象具有指向后方的构造函数和指向对象对象原型的 __ proto __ 属性

I know that the Animal function has a prototype property which point to it's prototype object. It's prototype object has constructor which points back and __proto__ property which points to Object object prototype

我知道每个函数对象都从 Function 的对象 Prototype 继承,并且每个函数对象都从Object的对象 Prototype 继承,包括 __ proto__属性.现在,当我进一步调查它时,我发现 Function的对象的 prototype __ proto __ 属性链接到相同的原型对象.

I am aware that every function object inherits from Function's object prototype and which inherit from Object's object prototype including __proto__ property. Now when I further investigated it, I found out that Function's object prototype and __proto__ property links to the same prototype object.

console.log(Animal.__proto__.constructor.__proto__  == Animal.__proto__.constructor.prototype ); //true
console.log(Animal.__proto__.constructor.__proto__ == Animal.__proto__); //true

然后我做了进一步的测试以证明这一点

Then I did some further test to prove it

Animal.__proto__.constructor.__proto__.test = 28;

console.log(Animal.__proto__.constructor.__proto__.test); // prints 28
console.log(Animal.__proto__.test);  //prints 28

这意味着它是原型,并且继承的原型是相同的.设置它的原因为何?

That means it's prototype and the prototype from it is inheriting are same. Any reason why it is set like that?

推荐答案

您已经知道 Object.getPrototypeOf(Animal)(或 Animal .__ proto __ )是 Function.prototype 对象.因此,让我们放下 Animal 东西,然后使用 Function.prototype 重复您的方程式:

You already know that Object.getPrototypeOf(Animal) (or Animal.__proto__) is the Function.prototype object. So let's drop that Animal thing and just repeat your equations with Function.prototype:

Function.prototype.constructor.__proto__ == Function.prototype.constructor.prototype // true
Function.prototype.constructor.__proto__ == Function.prototype // true

现在, Function.prototype .constructor 属性(类似于所有定义良好的原型对象)指向其各自的构造函数 Function 构造函数.所以我们有

Now, the .constructor property of Function.prototype (like for all well-defined prototype objects) points to its respective constructor, the Function constructor function. So we've got

Function.__proto__ == Function.prototype // true
Function.__proto__ == Function.prototype // true

现在,考虑到 Function 是一个函数,就像所有其他函数一样,它仅从 Function.prototype 继承是有意义的.

Now, given that Function is a function, it only makes sense that it inherits from Function.prototype like all other functions do.

这就是您的测试所证实的,就像您基本上所做的一样

This is what your testing confirms, as you basically did

Function.__proto__.test = 28;

Function.__proto__.test // 28
Function.prototype.test // 28

是的,现在 Function.test Animal.test 也会产生 28 .

And yes, Function.test and Animal.test would yield 28 as well now.

这篇关于函数对象__proto__和原型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:44