Question 56
Question
Answer
const myObject = {};
Object.defineProperty(myObject, 'value', {
get: function() {
console.log('Getting the value...');
return this._value; // Access a private property _value
},
set: function(newValue) {
console.log('Setting the value...');
this._value = newValue;
}
});
myObject._value = 'Initial Value';
console.log(myObject.value); // Outputs "Getting the value...", then "Initial Value"
myObject.value = 'New Value';
console.log(myObject.value); // Outputs "Setting the value..."Last updated