Created
April 17, 2017 16:57
-
-
Save bilalucar/f5c1a2b934ff5662f73c042025ca4e6e to your computer and use it in GitHub Desktop.
Operations inside getters and setters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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