Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  121] [ 6]  / answers: 1 / hits: 40154  / 14 Years ago, mon, march 7, 2011, 12:00:00

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?


More From » prototype

 Answers
44

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();

[#93393] Saturday, March 5, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sergiobrayanc

Total Points: 162
Total Questions: 97
Total Answers: 94

Location: Lesotho
Member since Wed, Jun 2, 2021
3 Years ago
;