Skip to content

Instantly share code, notes, and snippets.

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
console.log('name' in user) // true
// Getter function
console.log( // I am Akash Rajvanshi
// Setter function = 'Abha'
console.log( // 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.