Baseline Widely available
La méthode handler.ownKeys()
est une trappe pour Object.getOwnPropertyNames()
.
const monster1 = {
_age: 111,
[Symbol("secret")]: "I am scared!",
eyeCount: 4,
};
const handler1 = {
ownKeys(target) {
return Reflect.ownKeys(target);
},
};
const proxy1 = new Proxy(monster1, handler1);
for (const key of Object.keys(proxy1)) {
console.log(key);
// Expected output: "_age"
// Expected output: "eyeCount"
}
Syntaxe
var p = new Proxy(cible, {
ownKeys: function (cible) {},
});
Paramètres
Le paramètre suivant est passé à la méthode ownKeys
. this
est lié au gestionnaire.
cible
L'objet cible.
La méthode ownKeys
doit renvoyer un objet énumérable.
La méthode handler.ownKeys()
est une trappe pour intercepter les opérations de Object.getOwnPropertyNames()
.
Si les invariants suivants ne sont pas respectés, le proxy renverra une exception TypeError
:
ownKeys
doit être un tableau.String
, soit un Symbol
.Dans l'exemple suivant, on intercepte l'action de Object.getOwnPropertyNames()
.
var p = new Proxy(
{},
{
ownKeys: function (target) {
console.log("appelée");
return ["a", "b", "c"];
},
},
);
console.log(Object.getOwnPropertyNames(p)); // "appelée"
// [ "a", "b", "c"]
L'exemple suivant ne respecte pas l'ensemble des invariants :
var obj = {};
Object.defineProperty(obj, "a", {
configurable: false,
enumerable: true,
value: 10,
});
var p = new Proxy(obj, {
ownKeys: function (cible) {
return [123, 12.5, true, false, undefined, null, {}, []];
},
});
console.log(Object.getOwnPropertyNames(p));
// TypeError est levée
Spécifications Compatibilité des navigateurs Voir aussi
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4