> From: jackalm...@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.
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss