Je souhaite rendre une propriété invocable ou non. Ainsi, par exemple:Rendre une propriété invocable ou non
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();
De cette façon, je pouvais faire:
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'
Le code ne ci-dessus ne fonctionnent actuellement la façon dont je veux. Est-il possible de faire quelque chose comme ça en JavaScript? Soit en utilisant Object.defineProperty
ou pas
Quel est le cas d'utilisation de ce comportement? –
@shambalambala J'ai fait une meilleure question pour cela ici: http://stackoverflow.com/questions/43923053/how-to-make-a-property-method-invokable-or-not – georgej