このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

handler.getPrototypeOf()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2020年1月⁩.

handler.getPrototypeOf() メソッドは、オブジェクトの [[GetPrototypeOf]] 内部メソッドに対するトラップです。Object.getPrototypeOf() などの操作で使用されます。

試してみましょう

const monster = {
  eyeCount: 4,
};

const monsterPrototype = {
  eyeCount: 2,
};

const handler = {
  getPrototypeOf(target) {
    return monsterPrototype;
  },
};

const proxy = new Proxy(monster, handler);

console.log(Object.getPrototypeOf(proxy) === monsterPrototype);
// 予想される結果: true

console.log(Object.getPrototypeOf(proxy).eyeCount);
// 予想される結果: 2

構文

js
new Proxy(target, {
  getPrototypeOf(target) {
  }
})

引数

次の引数は getPrototypeOf() メソッドに渡されます。 this はハンドラーにバインドされます。

target

ターゲットオブジェクトです。

返値

getPrototypeOf() メソッドは、対象とするオブジェクトのプロトタイプを表すオブジェクトまたは null を返す必要があります。

解説

介入

このトラップは下記の操作に介入できます。

他にも、[[GetPrototypeOf]] 内部メソッドを呼び出すあらゆる操作に介入できます。

不変条件

プロキシーの [[GetPrototypeOf]] 内部メソッドは、ハンドラー定義が以下のいずれかの不変条件に違反する場合、TypeError が発生します。

  • 結果は、Object または null のどちらかでなければなりません。
  • 対象のオブジェクトが拡張可能でない場合(つまり、 Reflect.isExtensible()target に対して false を返す場合)、結果は Reflect.getPrototypeOf(target) の結果と同じでなければなりません。

基本的な使い方

js
const obj = {};
const proto = {};
const handler = {
  getPrototypeOf(target) {
    console.log(target === obj); // true
    console.log(this === handler); // true
    return proto;
  },
};

const p = new Proxy(obj, handler);
console.log(Object.getPrototypeOf(p) === proto); // true

getPrototypeOf トラップが発生する5つの方法

js
const obj = {};
const p = new Proxy(obj, {
  getPrototypeOf(target) {
    return Array.prototype;
  },
});
console.log(
  Object.getPrototypeOf(p) === Array.prototype, // true
  Reflect.getPrototypeOf(p) === Array.prototype, // true
  p.__proto__ === Array.prototype, // true
  Array.prototype.isPrototypeOf(p), // true
  p instanceof Array, // true
);

2 種類の例外

js
const obj = {};
const p = new Proxy(obj, {
  getPrototypeOf(target) {
    return "foo";
  },
});
Object.getPrototypeOf(p); // TypeError: "foo" is not an object or null

const obj2 = Object.preventExtensions({});
const p2 = new Proxy(obj2, {
  getPrototypeOf(target) {
    return {};
  },
});
Object.getPrototypeOf(p2); // TypeError: expected same prototype value

仕様書

Specification
ECMAScript® 2026 Language Specification
# sec-proxy-object-internal-methods-and-internal-slots-getprototypeof

ブラウザーの互換性

関連情報