I want to make a property either invokable or not. So for example:
function Test () {
var obj = { someString: 'here is text' };
Object.defineProperty(obj, 'string', {
get: function() {
return obj.someString;
},
set: function() {
return function(val) {
obj.someString = val;
}
}
});
return obj;
}
var test = new Test();
That way I could do:
test.string // initially returns 'here is text'
test.string('new text here') // sets obj.someString to 'new text here'
test.string // returns 'next text here'
The code above does not currently function the way I want it to. Is there anyway to do something like this in JavaScript? Either using Object.defineProperty
or not