Skip to content

Instantly share code, notes, and snippets.

@AkashRajvanshi
Created November 2, 2019 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkashRajvanshi/c19219767f9c6aef78936043d4580301 to your computer and use it in GitHub Desktop.
Save AkashRajvanshi/c19219767f9c6aef78936043d4580301 to your computer and use it in GitHub Desktop.
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