什么是函数的__proto__? [英] What is Function's __proto__?

查看:532
本文介绍了什么是函数的__proto__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个漂亮的图表和我在Chrome浏览器中做了一些测试,但我不知道如何解释:

I see this nice diagram and I've done some tests in my Chrome browser, but I don't know how to explain this:

> Function.prototype
  function Empty() {}
> Function.__proto__
  function Empty() {}
> typeof(Empty)
  "undefined"

什么是函数Empty(){} ,以及为什么 Function.prototype 是一个函数而不是 object 就像 Object.prototype

What is the function Empty() {}, and why Function.prototype is a function not a object just like Object.prototype?

从图中上面,似乎JavaScript中的所有内容都是从 Object.prototype 开始的,我是对的吗?

From the diagram above, it seems everything in JavaScript starts from Object.prototype, am I right about that?

推荐答案

首先,函数Empty(){} 表示是 V8 东西。

在V8中, Function.prototype 对象有清空作为 Function.prototype.name 属性的值,所以我猜你可能正在使用Chrome的开发者控制台,它以这种方式显示函数的名称。

In V8, the Function.prototype object has "Empty" as the value of the Function.prototype.name property, so I guess you are probably using the Chrome's Developer Console, and it displays the name of the function in this way.

函数对象的名称属性是非标准(不属于ECMA-262),这就是我们看到差异的原因实现。

The name property of function objects is non-standard (not part of ECMA-262), that's why we see differences between implementations.

现在, Function.prototype 是一个函数,它始终返回 undefined 并且可以接受任意数量的参数,但为什么呢?也许只是为了保持一致性,每个内置构造函数的原型都是这样的, Number.prototype 是一个 Number 对象, Array.prototype 是一个数组对象, RegExp.prototype 是一个 RegExp 对象,等等...

Now, Function.prototype is a function, that returns always undefined and can accept any number of arguments, but why?. Maybe just for consistency, every built-in constructor's prototype is like that, Number.prototype is a Number object, Array.prototype is an Array object, RegExp.prototype is a RegExp object, and so on...

唯一的区别(例如,任何函数对象和显然 Function.prototype 继承自 Object.prototype

The only difference (for example, between any function object and Function.prototype) is that obviously Function.prototype inherits from Object.prototype.


似乎javascript中的所有内容都是从Object.prototype开始的,我是对的吗?

it seems everything in javascript start from Object.prototype, am I right about that?

嗯,你是对的 Object.prototype 是大多数对象原型链的最后一个对象,但是在ECMAScript 5中,你甚至可以创建不从任何东西继承的对象(就像 Object.prototype 那样),并形成另一个继承链,例如:

Well, you're right Object.prototype is the last object of the prototype chain of most objects, but in ECMAScript 5, you can even create objects that doesn't inherit from anything (just like Object.prototype is), and form another inheritance chain, e.g.:

var parent = Object.create(null),
    child = Object.create(parent);

Object.prototype.isPrototypeOf(parent);  // false

Object.getPrototypeOf(parent);           // null
Object.getPrototypeOf(Object.prototype); // null

这篇关于什么是函数的__proto__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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