Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

Object.getPrototypeOf()

JavaScript Object.getPrototypeOf()方法

JavaScript Object.getPrototypeOf()方法返回原型(即,内部[[

语法:

Object.getPrototypeOf(obj)

参数

obj :这是要返回其原型的对象。

返回值:

此方法返回给定对象的原型。如果没有继承的属性,则此方法将返回null。

浏览器支持:

Chrome 5
Edge
Firefox 3.5
Opera 12.1

示例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

示例2

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

示例3

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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4