在JavaScript中,什么是最终的根,对象或函数? [英] In JavaScript, what is the ultimate root, Object or Function?
问题描述
例如,在以下源代码中 objects-functions-and-prototypes-in.html ,首先作者说:Javascript中的每个实体都是一个对象,后来它说任何新对象只能被创建为一个实例(即使你做'var a = new Object',Object是一个函数btw)。据我所知,作者基本上是自相矛盾的。
在JavaScript上的其他许多资源中,我看到了相同的混乱评论。在Java中很容易,你知道任何一个类的第一个实体是Hierarchy是Object类,但是在JavaScript中,我看到的只是混沌。
澄清如果对象先来或功能?什么是根。
我认为该行的结尾是 Object.prototype
,这是一个对象。这是让我这么想的:
Function.prototype; //空函数对象
Function.prototype .__ proto__; //默认的对象原型
Object.prototype; //默认的对象原型
Object.prototype .__ proto__; // null
ECMAScript 5.1规范声明如下:
-
在 15.3.4函数原型对象的属性:
$ b
Function原型对象的[[Prototype]]内部属性的值是标准内置Object原型对象
-
并且在 15.2。 4对象原型对象的属性
Object原型对象的[[Prototype]]内部属性的值是
null
In JavaScript is Object the root of all entities or Function is the root?
For example in the following source objects-functions-and-prototypes-in.html, first the author says "Every entity in Javascript is an object", and later it says "any new object can only be created as an instance of a function (even when you do 'var a = new Object;', Object is a function btw)". The author is basically contradicting himself as far as I can see.
And I see the same chaotic comments in so many other resources on JavaScript. In Java it is easy, you know the first entity in any class Hierarchy is the Object class, but in JavaScript, all I see is chaos.
So, can someone please clarify if Object comes first or Function? What is the root.
I believe the end of the line is Object.prototype
, which is an object. This is what makes me think so:
Function.prototype; // the empty function object
Function.prototype.__proto__; // the default Object prototype
Object.prototype; // the default Object prototype
Object.prototype.__proto__; // null
The ECMAScript 5.1 specification states it like this:
In 15.3.4 Properties of the Function Prototype Object:
The value of the [[Prototype]] internal property of the Function prototype object is the standard built-in Object prototype object
And in 15.2.4 Properties of the Object Prototype Object
The value of the [[Prototype]] internal property of the Object prototype object is
null
这篇关于在JavaScript中,什么是最终的根,对象或函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!