const fox = { _name: 'Oliver', _type: 'Fox' }; Object.defineProperty(fox, 'name', { enumerable: true, configurable: true, get: function() { return `${this._type}: ${this._name}`; }, set: function(name) { if (!this.previousNames) { this.previousNames = []; } this.previousNames.push(this._name); this._name = name; } });
fox.name = 'John';
fox.name = 'Doe'; console.log(
fox.name); // Fox: Doe console.log(`Current name: ${fox._name}. Previous names: ${fox.previousNames.join(', ')}`); // Current name: Doe. Previous names: Oliver, John