Proposal: Concise instance initialisation

Sean Eagan seaneagan1 at gmail.com
Tue May 24 07:02:12 PDT 2011


On Mon, May 23, 2011 at 4:44 PM, Rick Waldron <waldron.rick at gmail.com> wrote:
> Please excuse me, I'm just looking for clarification - thanks in advance:
>
>>
>> Also, the current infix writable mark does not compose with "Implicit
>> property initialization expressions" [1] as noted in [2].  Using a
>> prefix fixes this:
>>
>> let a = {!b}
>
> This conflicts with the existing:
> var foo = "bar";
> {foo}  // "bar"
> {!foo} // false
>

No, just because |{!b}| is current syntax, doesn't mean that |var a = {!b}| is.

>>
>> I don't think that's an issue, these all look backward compatible to me:
>>
>> !a.b = c
>> !a[b] = c
>> ~a.b = c
>> ~a.b += c
>> #a.b = c
>> #!~ a[b] *= c
>>
>
> Ignoring the lines prefixed "#"; all of these are currently invalid left
> hand assignment reference errors. Take away the assignments and the first
> two are negations, the second two bitwise not operations. So I'm wondering
> if they become "backward compatible" by simply becoming allowed, parsable
> syntax?
>

Yes, I am proposing new syntax and to my knowledge it is "backward
compatible" which means that it does not change the semantics of any
existing syntax.

>>
>> Also, if destructuring were then extended to allow the individual
>> LHS's to be property references rather than just Identifiers,
>> something like this would work:
>>
>> var [a, #~this.b, !this.c] = arguments;
>>
>
> Forgive me if I've misunderstood, assuming "#~" is somehow allowable, the
> third would still be negated with logical not, right?

I'm proposing to extend destructuring [1] to allow arbitrary LValues
(see [1]) rather than just Identifiers even when the destructuring
pattern is prefixed with a binding modifier ("var", "let", or "const")
and only use the binding modifier on the LValues that are Identifiers.
 So, starting from a function definition, here is essentially how the
desugaring would unfold:

function A (a, #~this.b, !this.c){
  foo(a);
}

// ->

function A (){
  var [a, #~this.b, !this.c] = arguments;
  foo(a);
}

// ->

function A (){
  var a = arguments[0];
  #~this.b = arguments[1];
  !this.c = arguments[2];
  foo(a);
}

// ->

function A (){
  var a = arguments[0];
  Object.defineProperty(this, "b", {configurable: false, enumerable:
false, writable: true, arguments[1]})
  Object.defineProperty(this, "c", {configurable: true, enumerable:
true, writable: false, arguments[2]})
  foo(a);
}

>
> Again, thanks for the clarification.
> Rick

[1] http://wiki.ecmascript.org/doku.php?id=harmony:destructuring

Thanks,
Sean Eagan


More information about the es-discuss mailing list