概述
Object.getPrototypeOf() 方法返回指定对象的原型(也就是该对象内部属性[[Prototype]]的值)。
语法
Object.getPrototypeOf(object)
参数
- object
- 返回该对象的原型。
示例:
var proto = {};
var obj = Object.create(proto);
Object.getPrototypeOf(obj) === proto; // true
注:
在 ES5 中,如果参数不是一个对象类型,将抛出一个 TypeError 异常。在 ES6 中,参数被强制转换为Object。
> Object.getPrototypeOf("foo");
TypeError: "foo" is not an object // ES5 code
> Object.getPrototypeOf("foo");
String.prototype // ES6 code
规范
| 规范版本 | 规范状态 | 注解 |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) Object.getPrototypeOf |
Standard | Initial definition. |
| ECMAScript 2015 (6th Edition, ECMA-262) Object.getProtoypeOf |
Standard |
浏览器兼容性
| Feature | Firefox (Gecko) | Chrome | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 3.5 | 5 | 9 | 12.10 (maybe earlier) | 5 |
| Feature | Firefox Mobile (Gecko) | Android | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | ? | ? | ? | ? | ? |
一些旧版本的Opera不支持标准的Object.getPrototypeOf()方法,但从10.50开始,Opera支持非标准的__proto__属性。
相关链接
Object.prototype.isPrototypeOfObject.setPrototypeOf- John Resig's post on getPrototypeOf