title | slug |
---|---|
Object.getPrototypeOf() |
Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf |
{{JSRef}}
Object.getPrototypeOf()
静态方法返回指定对象的原型(即内部 [[Prototype]]
属性的值)。
{{EmbedInteractiveExample("pages/js/object-getprototypeof.html", "shorter")}}
Object.getPrototypeOf(obj)
obj
- : 要返回其原型的对象。
给定对象的原型,可能是 null
。
const proto = {};
const obj = Object.create(proto);
Object.getPrototypeOf(obj) === proto; // true
在 ES5 中,如果 obj
参数不是对象,则会抛出 {{jsxref("TypeError")}} 异常。在 ES2015 中,该参数将被强制转换为 {{jsxref("Object")}}。
Object.getPrototypeOf("foo");
// TypeError: "foo" is not an object (ES5 code)
Object.getPrototypeOf("foo");
// String.prototype (ES2015 code)
{{Specifications}}
{{Compat}}
core-js
中Object.getPrototypeOf
的 polyfill- {{jsxref("Object.prototype.isPrototypeOf()")}}
- {{jsxref("Object.setPrototypeOf()")}}
Object.prototype.__proto__
- John Resig 关于 getPrototypeOf 的博文。
- {{jsxref("Reflect.getPrototypeOf()")}}