Object.getPrototypeOf() 方法返回指定对象的原型(内部[[Prototype]]属性的值)。
Object.getPrototypeOf(object)
objnull 。var proto = {};
var obj = Object.create(proto);
Object.getPrototypeOf(obj) === proto; // true
var reg = /a/;
Object.getPrototypeOf(reg) === RegExp.prototype; // true
Object.getPrototypeOf(Object) 不是 Object.prototype
JavaScript中的 Object 是构造函数(创建对象的包装器)。
一般用法是:
var obj = new Object();
所以:
Object.getPrototypeOf( Object ); // ƒ () { [native code] }
Object.getPrototypeOf( Function ); // ƒ () { [native code] }
Object.getPrototypeOf( Object ) === Function.prototype; // true
Object.getPrototypeOf( Object )是把Object这一构造函数看作对象,
返回的当然是函数对象的原型,也就是 Function.prototype。
正确的方法是,Object.prototype是构造出来的对象的原型。
var obj = new Object();
Object.prototype === Object.getPrototypeOf( obj ); // true
Object.prototype === Object.getPrototypeOf( {} ); // true
在 ES5 中,如果参数不是一个对象类型,将抛出一个TypeError异常。在 ES2015 中,参数会被强制转换为一个 Object。
Object.getPrototypeOf('foo');
// TypeError: "foo" is not an object (ES5 code)
Object.getPrototypeOf('foo');
// String.prototype (ES2015 code)
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) Object.getPrototypeOf |
Standard | Initial definition. |
| ECMAScript 2015 (6th Edition, ECMA-262) Object.getPrototypeOf |
Standard | |
| ECMAScript Latest Draft (ECMA-262) Object.getPrototypeOf |
Draft |
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
getPrototypeOf | Chrome Full support 5 | Edge Full support 12 | Firefox Full support 3.5 | IE Full support 9 | Opera Full support 12.1 | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support Yes |
即使旧版本Opera不支持Object.getPrototypeOf(),Opera 10.50之后还支持非标准的 __proto__属性。