Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Operations inside getters and setters
var obj = {
n: 67,
get id() {
return 'The ID is: ' + this.n;
},
set id(val) {
if (typeof val === 'number')
this.n = val;
}
}
console.log(obj.id);
// "The ID is: 67"
obj.id = 893;
console.log(obj.id);
// "The ID is: 893"
obj.id = 'hello';
console.log(obj.id);
// "The ID is: 893"
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.