Object.getPrototypeOf()【プロトタイプ 取得】メソッド

メモ

  • プロトタイプを取得

構文

  • Object.getPrototypeOf ( O )

  • O:オブジェクト (オブジェクト以外はオブジェクト変換 )

プロトタイプ

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

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

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

var size = { width:10, height:20 };
var 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

関連