Re: EcmaScript Proposal – Private methods and fields proposals.

Sultan thysultan at gmail.com
Tue Apr 17 21:26:20 UTC 2018


> In the transpilation you created the field using "registry.set(this, {id:
0})"
>in the constructor.  If you then claim that any write to the field can
also create it, then you get the hijacking behavior which you wrote doesn't
happen.

The difference between

class A {
  private id = 0
}

and

class A {
  constructor() {
    private.id = 0
  }
}

is the likened to the difference between

(function (){
  var registry = WeakMap()

  function A () {
    registry.set(this, {id: 0})
  }

  return A
})()

and

(function () {
  var registry = WeakMap()

  function A () {
    registry.set(this, {})
    registry.get(this)["id"] = 0
  }

  return A
})

I don't see how this permits the hijacking behavior previously mentioned,
that is –

(new A()).write.call({}, 'pawned');

Would still fail in the same way for both of these variants.

>They just lexically scope the private names in their own separate
namespace.  #foo refers to the innermost enclosing class that has a private
field called foo.

I'm not sure i understand, Does #foo refer to this.#foo? Can you post a
fleshed out example of this?

On Wed, Apr 18, 2018 at 12:09 AM, Waldemar Horwat <waldemar at google.com>
wrote:

> On 04/17/2018 01:50 PM, Sultan wrote:
>
>>  >That would contradict your previous answer to the hijacking question.
>>
>> Can you point out the contradiction? The private field is still being
>> written to by the providing class.
>>
>
> In the transpilation you created the field using
>
>   registry.set(this, {id: 0})
>
> in the constructor.  If you then claim that any write to the field can
> also create it, then you get the hijacking behavior which you wrote doesn't
> happen.
>
> Class B is lexically nested inside class A. You want to refer to one of
>>> A's privates from within B's body.
>>>
>>
>> Can you provide an example of what this looks like with the current
>> public/private fields proposals?
>>
>
> They just lexically scope the private names in their own separate
> namespace.  #foo refers to the innermost enclosing class that has a private
> field called foo.
>
>     Waldemar
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180418/90f705d4/attachment.html>


More information about the es-discuss mailing list