getter & setter for private objects

Mike Samuel mikesamuel at gmail.com
Mon Nov 2 10:47:24 PST 2009


What do getters and setters do around with?

   var flipFlop;
   with ({ get test: function () { return (flipFlop = !flipFlop) ?
'flip' : 'flop'; } }) {
     for (var i = 0; i < 10; ++i) { alert(test); }
   }

The with block invokes NewObjectEnvironment with O being the binding object.
But I'm unclear on whether line 4 in GetBindingValue below would
invoke a getter.

=== start quote ===
10.2.2.3 NewObjectEnvironment (O, E)
When the abstract operation NewObjectEnvironmentis called with an
Object O and a Lexical Environment E
(or null) as arguments, the following steps are performed:
1. Let env be a new Lexical Environment.
2. Let envRec be a new object environment record containing O as the
binding object.
3. Set env’s environment record to be envRec.
4. Set the outer lexical environment reference of env to E.
5. Return env.

10.2.1.1.4 GetBindingValue(N,S)
The concrete Environment Record method GetBindingValue for declarative
environment records simply
returns the value of its bound identifier whose name is the value of
the argument N. The binding must already
exist. If S is true and the binding is an uninitialized immutable
binding throw a ReferenceError exception.
1. Let envRec be the declarative environment record for which the
method was invoked.
2. Assert: envRec has a binding for N.
3. If the binding for N in envRec is an uninitialized immutable binding, then
a. If S is false, return the value undefined, otherwise throw a
ReferenceError exception.
4. Else, return the value currently bound to N in envRec.
=== end quote ===

Does 4

2009/11/2 Brendan Eich <brendan at mozilla.com>:
> 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
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list