JSFiddle - React, Tailwind, and code Playground
JavaScript
var superhero = {
id: 0,
firstname: 'Duck',
lastname: 'Darkwing'
};
for (property in superhero)
{
var propertyName = property.toString();
// Store initial value in '_var' before overwriting it with get/set
this['_' + propertyName] = superhero['_' + propertyName] = superhero[property];
Object.defineProperty(superhero, propertyName, {
set: function(value) {
this['_' + propertyName] = value;
},
get: function() {
return this['_' + propertyName];
}
});
}
superhero.firstname = 'Kal';
superhero.lastname = 'El';
alert(superhero.firstname + ' ' + superhero.lastname);