JS 对象机制深剖——Object.prototype 是谁?

来源:互联网 发布:淘宝信誉在哪里看 编辑:程序博客网 时间:2024/05/17 03:40

转自:http://hi.baidu.com/lansesansan/item/9fb39d09f730913c4ac4a3d0

我们通过 new Object 或直接量 {} 得到一个空白对象,然而此对象并非“一无所有”,它已经具备如下属性或方法:

  • constructortoString()toLocaleString()valueOf()hasOwnProperty()isPrototypeOf()propertyIsEnumerable()

以上属性不可枚举,不可删除,并且只读。那么,它们由谁提供?没错,它们来自 Object.prototype。

ECMAScript 语言规范的提法十分明确:

The initial(最初的) value of Object.prototype is the Object prototype object.

“the Object prototype object”就是 Object.prototype 的“标准名称”,够囧吧。这还没完,继续考察这个 the Object prototype object:

The value of the internal [[Prototype]] property of the Object prototype object is null and the value of the internal [[Class]] property is “Object”.

规范指出,在引擎内部,这个对象的 [[Prototype]] 属性指向空,于是可以知道,它已经处于原型链的顶层,而为了对象系统的完备性,自身还是有一个 [[Prototype]] 内部属性,并指向同为“Object Type”的 NULL。

至此,原型链系统完美地画上一个句号。我们做一个总结:JS 1.x 中,Object 是顶层构造器,Object.prototype 是顶层对象实例,同时也是原型链的祖宗,同时,我们尊称这位祖宗为“the Object prototype object”!


原创粉丝点击