getter & setter for private objects
Brendan Eich
brendan at mozilla.com
Mon Nov 2 10:17:41 PST 2009
On Nov 2, 2009, at 2:34 AM, memolus at googlemail.com wrote:
> I like to use getter and setter on private objects (e.g. "var test").
That is very unclear; I'm guessing you mean getters and setters
instead of plain variables in closures used to store private data.
> I didn't found any way to do this. I don't need sth. like
> Object.definePrivateProperty, because I only want to set getter and
> setter when I define the private object.
>
> Example proposal:
> var test;
> get test = function() { [..] };
> set test = function(newval) { [..] };
>
> I'll be pleased to hear your feedback.
Defining accessors on an activation object is nasty, it makes
analyzing for effects hard (consider the arguments object, which is an
indirect accessor -- both get and set -- of formal parameter values).
I doubt we'll ever standardize any such thing; Mozilla would not
implement it even experimentally.
If you want private getters and setters, you can put them in an object
denoted by a private var:
function make() {
var obj = {
get test() /* cheap fetch of x here */,
set test(x) /* cheap update from x here */
};
function validate(x) { /* expensive check of x here */ }
return {
get test() { return obj.test; },
set test(x) { validate(x); obj.test = x; }
// etc.
};
}
/be
More information about the es-discuss
mailing list