Proposal About Private Symbol

Gary Guo nbdd0121 at hotmail.com
Sun Dec 21 03:00:00 PST 2014


> From: jackalmage at gmail.com
> Date: Sun, 21 Dec 2014 00:45:40 -0800>
> There are ways around this, too - create your own Symbol, keep it
> closure-private to the class, so it doesn't escape the class's
> methods.  Have the constructor take an extra argument that must be
> equal to this symbol; if it's not, the constructor instead calls
> itself, passing all of its arguments through and adding the special
> identity Symbol, and just returns that.  That way you know that your
> "this" object is definitely a fresh language-created one, and you're
> definitely being called as a constructor, not as a function invoked on
> some arbitrary object.

There is already a way to workaround like this in ES5.1 to ensure constructor are called as expected```jsfunction packAsConstructor(construct){    'use strict';    return function(){        if(this===undefined)throw Error('Invalid call as non-constructor');        if(construct.prototype) Object.setPrototypeOf(this, construct.prototype);        return construct.apply(this, arguments);    }.bind(undefined);}```
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. 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141221/a7d29697/attachment.html>


More information about the es-discuss mailing list