Proposal About Private Symbol

Domenic Denicola d at domenic.me
Sun Dec 21 09:37:48 PST 2014


Yes, if you use weak sets in combination with private symbols you can more or less emulate weak maps. But why not just use weak maps in that case?

From: es-discuss [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Gary Guo
Sent: Sunday, December 21, 2014 07:26
To: es-discuss at mozilla.org
Subject: RE: Proposal About Private Symbol

Oops, I forget the WeakSet. So seems my private symbol proposal can work now. Under very deliberately design, private symbol can be used as private field.

```js
var constructor=function(){
    'use strict';
    var allObjects=new WeakSet();
    var privateSymbol=Symbol('private', true);
    var ret=function(){
        if(this===undefined)throw Error('Invalid Construction');
        this[privateSymbol]=1;
        allObjects.add(this);
    }
    ret.prototype.set=function(sth){
        if(!allObject.has(this))throw Error('Invalid Call');
        this[privateSymbol]=sth; // Now this can be called safely, no more worry about leak to Proxy
    }
    ret.bind(undefined);
}
```

On Sun, 21 Dec 2014 12:30:46 +0100, Michal Wadas <michalwadas at gmail.com<mailto:michalwadas at gmail.com>> wrote:
>> But this is not the core of the problem. The problem is the Proxy introduced in ES6 enables an object to capture and override almost any operation on an object. Without operation on object, it becomes very costly (by using an Array of created objects and compare each of them) to identify whether a object is faked or valid.
>ES6 WeakSet provides you O(1) object check and don't cause memory leak.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141221/797cdf82/attachment-0001.html>


More information about the es-discuss mailing list