Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const user = {
_firstName: 'Akash',
_lastName: 'Rajvanshi'
};
Object.defineProperty(user, 'name', {
get: function() {
return `I am ${this._firstName} ${this._lastName}`;
},
set: function(value) {
console.log('Setting Name To : %s', value); // When setting another value to _firstName -> "Setting Name To : Abha"
this._firstName = value;
},
enumerable: false,
configurable: false
});
// Enumerable
console.log('name' in user) // true
console.log(user.propertyIsEnumerable('name')) // false
// Configurable
delete user.name
console.log('name' in user) // true
// Getter function
console.log(user.name) // I am Akash Rajvanshi
// Setter function
user.name = 'Abha'
console.log(user.name) // I am Abha Rajvanshi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.