Re: EcmaScript Proposal – Private methods and fields proposals.
Waldemar Horwat
waldemar at google.com
Mon Apr 16 22:43:21 UTC 2018
On 04/13/2018 09:41 PM, Sultan wrote:
> >Writing your private field to an object that's not an instance of your class.
> >and then invoking the above write method with a this value that's not an instance of A, such as a proxy.
>
> Given:
>
> class A {
> private id = 0;
> private method(value) {
> return value;
> }
> write(value) {
> private(this)["id"] = private["method"](value);
> }
> }
>
> I imagine this means trying to do something along the lines of:
>
> (new A()).write.call({}, 'pawned');
>
> This would fail. The private syntax call site would be scoped to the provider class. For example imagine the current possible transpilation of this:
>
> ;(function (){
> var registry = WeakMap();
>
> function A () {
> registry.set(this, {id: 0})
> }
> A.prototype.write: function () {
> registry.get(this)["id"] = registry.get(this.constructor)["method"].call(this, value);
> }
>
> // shared(i.e private methods)
> registry.set(A, {
> method: function (value) {
> return value;
> }
> });
>
> return A
> })();
>
> Trying to do the the afore-mentioned forge here would currently fail along the lines of cannot read property "id" of "undefined".
OK, so that aspect of the proposal looks the same as the existing private proposals — an instance has a fixed set of private fields which get created at object creation time. There are tricky additional wrinkles when it comes to inheritance, but you can look them up in the existing proposals.
Are the only significant changes the different property naming syntax and that you provide a way to map strings to private slots? How do you deal with inner nested classes wanting to refer to outer classes' private fields?
Waldemar
More information about the es-discuss
mailing list