Determining if an object can be constructed

Isiah Meadows isiahmeadows at gmail.com
Tue Jan 17 22:48:03 UTC 2017


Out of curiosity, why are classes specified to have a [[Call]] internal
method instead of special-casing `typeof` and friends to work with them?
Somewhat of a tangent, but just a curious question on the design decision.

On Tue, Jan 17, 2017, 08:31 Claude Pache <claude.pache at gmail.com> wrote:

>
> > Le 16 janv. 2017 à 18:00, Michael Haufe <tno at thenewobjective.com> a
> écrit :
> >
> > The question came up recently on CLJS [1] on how to determine if an
> object is safe to call as a Constructor.
>
> The two following procedures will determine whether an object is a
> constructor without running it, according to ES6. (Whether it is safe to
> call it as constructor... you need first a definition of "safe".)
>
> (1) Using `class/extends`:
>
> ```js
> function isConstructor1(f) {
>     if (f === null)
>         return false
>     try {
>         class c extends f { }
>         return true
>     }
>     catch (e) {
>        return false
>     }
> }
> ```
>
> (2) Using `Proxy`:
>
> ```js
> function isConstructor2(f) {
>     var p = new Proxy(f, { construct() { return {} } })
>     try {
>         new p
>         return true
>     }
>     catch (e) {
>        return false
>     }
> }
> ```
>
>
> —Claude
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170117/5f32414f/attachment.html>


More information about the es-discuss mailing list