Skip to content

Instantly share code, notes, and snippets.

@bilalucar
Created April 17, 2017 16:57
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 bilalucar/f5c1a2b934ff5662f73c042025ca4e6e to your computer and use it in GitHub Desktop.
Save bilalucar/f5c1a2b934ff5662f73c042025ca4e6e to your computer and use it in GitHub Desktop.
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