Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
const Serializable = (Base) => class extends Base { serialize() { return JSON.stringify(this); } static deserialize(json) { return Object.assign(new this(), JSON.parse(json)); } }; const Loggable = (Base) => class extends Base { log(msg) { console.log("[" + this.constructor.name + "] " + msg); } }; class Entity { constructor(id) { this.id = id; } } class User extends Serializable(Loggable(Entity)) { constructor(id, name) { super(id); this.name = name; } } const u = new User(1, 'Alice'); u.log('created'); // [User] created console.log(u.serialize()); // JSON string console.log(u instanceof Entity); // true
Result
Open