Reflect.deleteProperty()

La méthode statique Reflect.deleteProperty() permet de supprimer des propriétés. Il agit comme l'opérateur delete.

Exemple interactif

Syntaxe

js
Reflect.deleteProperty(cible, cléPropriété);

Paramètres

cible

L'objet cible sur lequel on souhaite supprimer la propriété.

cléPropriété

Le nom de la propriété à supprimer.

Valeur de retour

Un booléen qui indique si la suppression de la propriété s'est bien passée.

Exceptions

Une erreur TypeError si cible n'est pas un Object.

Description

La méthode Reflect.deleteProperty permet de supprimer une propriété d'un objet. Elle renvoie un Boolean qui indique si la propriété a été supprimée correctement. Cette méthode est très proche de l'opérateur delete.

Exemples

js
var obj = { x: 1, y: 2 };
Reflect.deleteProperty(obj, "x"); // true
obj; // { y: 2 }

var arr = [1, 2, 3, 4, 5];
Reflect.deleteProperty(arr, "3"); // true
arr; // [1, 2, 3, , 5]

// Renvoie true si aucune propriété correspondante n'existe
Reflect.deleteProperty({}, "toto"); // true

// Renvoie false si une propriété n'est pas configurable
Reflect.deleteProperty(Object.freeze({ toto: 1 }), "toto"); // false

Spécifications

Specification
ECMAScript Language Specification
# sec-reflect.deleteproperty

Compatibilité des navigateurs

BCD tables only load in the browser

Voir aussi