The handler.getPrototypeOf()
method is a trap for the [[GetPrototypeOf]]
internal method.
var p = new Proxy(obj, { getPrototypeOf(target) { ... } });
The following parameter is passed to the getPrototypeOf
method. this
is bound to the handler.
target
The getPrototypeOf
method must return an object or null
.
This trap can intercept these operations:
Object.getPrototypeOf()
Reflect.getPrototypeOf()
__proto__
Object.prototype.isPrototypeOf()
instanceof
If the following invariants are violated, the proxy will throw a TypeError
:
getPrototypeOf
method must return an object or null
.target
is not extensible, Object.getPrototypeOf(proxy)
method must return the same value as Object.getPrototypeOf(target)
.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
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 );
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
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 |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | No | No | 49 | No | No | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | No | No | No | 49 | No | No | No |
Server | |
---|---|
Node.js | |
Basic support | 6.0.0 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/getPrototypeOf