I recently read about the fact that there is a possibility of defining getters/setters in JavaScript. It seems extremely helpful - the setter is a kind of 'helper' which can parse the value to be set first, before actually setting it.
For example, I currently have this code:
var obj = function(value) {
var test = !!value; // 'test' has to be a boolean
return {
get test() { return test },
set test(value) { test = !!value }
};
};
var instance = new obj(true);
This code always converts value
to a boolean. So if you code instance.test = 0
, then instance.test === false
.
However, for this to work you have to actually return an object, which means that the new instance is not of type obj
but just is a plain object. This means that changing the prototype of obj
has no effect on instances. For example, this does not work - instance.func
is undefined:
obj.prototype.func = function() { console.log(this.value); };
because instance
is not of type obj
. To get the prototype functions work, I guess I should not return a plain object, but rather not return anything so that instance
would just be of type obj
, like a regular constructor works.
The problem then is how to implement getters/setters? I can only find articles describing how to add these to an object, not as being part of the constructor of a custom type.
So how do I implement getters/setters in the constructor so as to be able to both use getters/setters and extending the prototype?
You can't do that.
You can set setter/getters for properties of objects though. I advice you use ES5 Object.defineProperties
though. of course this only works in modern browsers.
var obj = function() {
...
Object.defineProperties(this, {
"test": {
"get": function() { ... },
"set": function() { ... }
}
});
}
obj.prototype.func = function() { ... }
var o = new obj;
o.test;
o.func();