Наши волонтёры ещё не перевели данную статью на Русский. Присоединяйтесь к нам и помогите сделать эту работу!
Вы можете также прочитать эту статью на English (US).

The handler.getPrototypeOf() method is a trap for the [[GetPrototypeOf]] internal method.

Syntax

var p = new Proxy(obj, {
  getPrototypeOf(target) {
  ...
  }
});

Parameters

The following parameter is passed to the getPrototypeOf method. this is bound to the handler.

target
The target object.

Return value

The getPrototypeOf method must return an object or null.

Description

Interceptions

This trap can intercept these operations:

Invariants

If the following invariants are violated, the proxy will throw a TypeError:

  • getPrototypeOf method must return an object or null.
  • If target is not extensible, Object.getPrototypeOf(proxy) method must return the same value as Object.getPrototypeOf(target).

Examples

Basic usage

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

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

Five ways to trigger the getPrototypeOf trap

var obj = {};
var 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
);

Two kinds of exceptions

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

var obj = Object.preventExtensions({});
var p = new Proxy(obj, {
    getPrototypeOf(target) {
        return {};
    }
});
Object.getPrototypeOf(p); // TypeError: expected same prototype value

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of '[[GetPrototypeOf]]' in that specification.
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
The definition of '[[GetPrototypeOf]]' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
getPrototypeOfChrome Full support 49Edge No support NoFirefox Full support 49IE No support NoOpera Full support 36Safari No support NoWebView Android Full support 49Chrome Android Full support 49Firefox Android Full support 49Opera Android Full support 36Safari iOS No support NoSamsung Internet Android Full support Yesnodejs Full support 6.0.0

Legend

Full support  
Full support
No support  
No support

See also

Метки документа и участники

Внесли вклад в эту страницу: mdnwebdocs-bot, jpmedley, wbamberg, fscholz, nmve, kdex, ziyunfei, evilpie, arai
Обновлялась последний раз: mdnwebdocs-bot,