JavaScript的 Object.getPrototypeOf()方法返回指定对象的原型(即内部[[Prototype]]属性的值)。
Object.getPrototypeOf(obj)
obj :这是要返回其原型的对象。
此方法返回给定对象的原型。如果没有继承的属性,则此方法将返回null。
Chrome | 5 |
Edge | yes |
Firefox | 3.5 |
Opera | 12.1 |
let animal = { eats: true }; //create a new object with animal as a prototype let rabbit = Object.create(animal); alert(Object.getPrototypeOf(rabbit) === animal);//get the prototype of rabbit Object.setPrototypeOf(rabbit, {});//change the prototype of rabbit to {}
输出:
true
const prototype1 = {}; const object1 = Object.create(prototype1); const prototype2 = {}; const object2 = Object.create(prototype2); console.log(Object.getPrototypeOf(object1) === prototype1); console.log(Object.getPrototypeOf(object1) === prototype2);
输出:
true false
const prototype1 = {}; const object1 = Object.create(prototype1); const prototype2 = {}; const object2 = Object.create(prototype2); console.log(Object.getPrototypeOf(object1) === prototype1); console.log(Object.getPrototypeOf(object2) === prototype2);
输出:
true true
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)