Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

handler.getPrototypeOf()

La méthode handler.getPrototypeOf() représente une trappe pour la méthode interne [[GetPrototypeOf]].

Syntaxe

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

Paramètres

Le paramètre suivant est passé à la méthode getPrototypeOf. this est lié au gestionnaire.

cible
L'objet cible.

Valeur de retour

La méthode getPrototypeOf doit renvoyer un objet ou null.

Description

Interceptions

Cette trappe permet d'intercepter les opérations suivantes :

Invariants

Si les invariants suivant ne sont pas respectés, le proxy renverra une exception TypeError :

  • getPrototypeOf doit renvoyer un objet ou null.
  • Si la cible n'est pas extensible, Object.getPrototypeOf(proxy) doit renvoyer la même valeur que Object.getPrototypeOf(cible).

Exemples

Utilisation simple

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

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

Cinq façons de déclencher la trappe getPrototypeOf

var obj = {};
var p = new Proxy(obj, {
    getPrototypeOf(cible) {
        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
);

Deux types d'exceptions

var obj = {};
var p = new Proxy(obj, {
    getPrototypeOf(cible) {
        return "toto";
    }
});
Object.getPrototypeOf(p); // TypeError : "toto" n'est pas un objet ou null

var obj = Object.preventExtensions({});
var p = new Proxy(obj, {
    getPrototypeOf(cible) {
        return {};
    }
});
Object.getPrototypeOf(p); // TypeError : on attend la même valeur pour le prototype

Spécifications

Spécification Statut Commentaires
ECMAScript 2015 (6th Edition, ECMA-262)
La définition de '[[GetPrototypeOf]]' dans cette spécification.
Standard Définition initiale.
ECMAScript 2017 Draft (ECMA-262)
La définition de '[[GetPrototypeOf]]' dans cette spécification.
Projet  

Compatibilité des navigateurs

Fonctionnalité Chrome Firefox (Gecko) Internet Explorer Opera Safari
Support simple Pas de support 49 (49) Pas de support Pas de support Pas de support
Fonctionnalité Android Chrome pour Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Support simple Pas de support Pas de support 49.0 (49) Pas de support Pas de support Pas de support

Voir aussi

Étiquettes et contributeurs liés au document

 Contributeurs à cette page : SphinxKnight
 Dernière mise à jour par : SphinxKnight,