Exemple
class Panier {
#articles = []; // propriété privée
constructor(client) { this.client = client; }
ajouter(nom, prix, qte = 1) { this.#articles.push({ nom, prix, qte }); return this; }
get total() { return this.#articles.reduce((s, a) => s + a.prix * a.qte, 0); }
static depuisJSON(json) { /* … */ }
}
class PanierPro extends Panier {
get total() { return super.total * 0.9; }
}
const p = new PanierPro("Léa").ajouter("Clavier", 49);Les classes JavaScript reposent sur les prototypes ; this dans un callback : utilisez une fonction fléchée. Équivalent Python : classes Python.