JavaScript Object Getters and Setters - scs-cs-50/presentations GitHub Wiki
var thing = function thing (init) {
var myinit = init,
that = {
get init () {
return myinit;
},
set init (newinit) {
if (newinit < 0 || newinit > 100) {
throw new Error('Way out of range');
}
myinit = newinit;
return myinit;
}
};
return that;
};
var x = thing(4);
console.log('x init: ' + x.init);
console.log('calling x.init = 6');
x.init = 6;
console.log('x init: ' + x.init);
console.log('calling x.init = -6');
x.init = -6;
console.log('x init: ' + x.init);