getPrototypeOf【プロトタイプ 取得】
isPrototypeOf【プロトタイプチェーン存在有無】

Object.getPrototypeOf【プロトタイプ 取得】

メモ

概要

  • プロトタイプを取得

関連

外部リンク

構文

Object.getPrototypeOf( O ) 

 プロトタイプ
O オブジェクト (オブジェクト以外はオブジェクト変換 )

TypeError 例外 O (オブジェクト)がオブジェクト以外 

const point = { x:10, y:20 };
const point2 = Object.create(point);
point2.z = 30;
console.log(point2.x, point2.y, point2.z);
// 出力:10 20 30

const line = Object.create(point);
line.x2 = 40;
line.y2 = 50;
console.log(line.x, line.y, line.x2, line.y2);
// 出力:10 20 40 50

const size = { width:10, height:20 };
const cuboid = Object.create(size);
cuboid.depth = 30;
console.log(cuboid.width, cuboid.height, cuboid.depth);
// 出力:10 20 30

console.log(Object.getPrototypeOf(point2));
// 出力:Object {x: 10, y: 20}
console.log(Object.getPrototypeOf(line));
// 出力:Object {x: 10, y: 20}
console.log(Object.getPrototypeOf(cuboid));
// 出力:Object {width: 10, height: 20}
console.log(Object.getPrototypeOf(point2) === Object.getPrototypeOf(line));
// 出力:true
console.log(Object.getPrototypeOf(point2) === Object.getPrototypeOf(cuboid));
// 出力:false

Object.prototype.isPrototypeOf【プロトタイプチェーン存在有無】

メモ

概要

  • 別オブジェクトのプロトタイプチェーンに存在するか判定

関連

外部リンク

構文

Object.isPrototypeOf( V )

 プロトタイプチェーン存在有無 (true:有り / false:無し)
V オブジェクト

function F1() {};
function F2() {};
function F3() {};
function F4() {};
F2.prototype = new F1();
const f2 = new F2();
F3.prototype = new F2();
const f3 = new F3();
const f4 = new F4();
console.log(F1.prototype.isPrototypeOf(f2));
// true
console.log(F1.prototype.isPrototypeOf(f3));
// true
console.log(F1.prototype.isPrototypeOf(f4));
// false