getter & setter for private objects
Brendan Eich
brendan at mozilla.com
Mon Nov 2 11:30:12 PST 2009
On Nov 2, 2009, at 10:47 AM, Mike Samuel wrote:
> 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.
It ought to, because ES3 with anything like getters, including a
native or host object cooperating [[HasProperty]] and [[Get]]
implementations -- but also Mozilla's getter and setter implementation
of ten years' standing, would invoke the getter.
ES3 spec sections are 12.10, 10.1.4, and 11.1.2 which feeds into
GetValue when the Identifier is used as a rvalue; GetValue does call
[[Get]].
Indeed ES5 is correctly compatible with ES3 + getters or internal
[[HasProperty]]/[[Get]] implementations.
> 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 ===
Wrong section. The concrete type of envRec for a 'with' statement is
an object environment record, so you want:
10.2.1.2.4 GetBindingValue(N,S)
The concrete Environment Record method GetBindingValue for object
environment records returns the value of its associated binding
object‘s property whose name is the String value of the argument
identifier N. The property should already exist but if it does not the
result depends upon the value of the S argument:
1. Let envRec be the object environment record for which the method
was invoked.
2. Let bindings be the binding object for envRec.
3. Let value be the result of calling the [[HasProperty]] internal
method of bindings, passing N as the property name.
4. If value is false, then
a. If S is false, return the value undefined, otherwise throw a
ReferenceError exception.
5. Return the result of calling the [[Get]] internal method of
bindings, passing N for the argument.
/be
More information about the es-discuss
mailing list