Object.getPrototypeOf()

Object.getPrototypeOf() 静态方法返回指定对象的原型(即内部 [[Prototype]] 属性的值)。

¥The Object.getPrototypeOf() static method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.

Try it

语法

¥Syntax

js
Object.getPrototypeOf(obj)

参数

¥Parameters

obj

要返回其原型的对象。

返回值

¥Return value

给定对象的原型,可能是 null

¥The prototype of the given object, which may be null.

示例

¥Examples

使用 getPrototypeOf

¥Using getPrototypeOf

js
const proto = {};
const obj = Object.create(proto);
Object.getPrototypeOf(obj) === proto; // true

非对象强制

¥Non-object coercion

在 ES5 中,如果 obj 参数不是对象,则会抛出 TypeError 异常。在 ES2015 中,该参数将被强制为 Object

¥In ES5, it will throw a TypeError exception if the obj parameter isn't an object. In ES2015, the parameter will be coerced to an Object.

js
Object.getPrototypeOf("foo");
// TypeError: "foo" is not an object (ES5 code)
Object.getPrototypeOf("foo");
// String.prototype                  (ES2015 code)

规范

Specification
ECMAScript Language Specification
# sec-object.getprototypeof

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看