Determining if an object can be constructed

Andrea Giammarchi andrea.giammarchi at gmail.com
Thu Jan 19 12:46:18 UTC 2017


If you are looking for isClass or similar you can also rely on
`Function.prototype.toString` decompilation, which is de facto consistent.

```js
const isClass = (fn) =>
  typeof fn === 'function' &&
  !isClass.toString.call(fn).indexOf('class ');
```

Of course if you transpile code, including classes, that'd be useless (but
then you'll have many other problems anyway)

Best Regards


On Thu, Jan 19, 2017 at 12:18 PM, Isiah Meadows <isiahmeadows at gmail.com>
wrote:

> Inline.
>
> On Thu, Jan 19, 2017, 03:26 Claude Pache <claude.pache at gmail.com> wrote:
>
>> Le 17 janv. 2017 à 23:48, Isiah Meadows <isiahmeadows at gmail.com> a écrit
>> :
>>
>> 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.
>>
>> I guess that class constructors could have been specified without a
>> [[Call]] internal method, with the cost of amending all the places where
>> “constructible” implicitly implies “callable”.
>>
>
> FWIW, calling methods that don't have [[Call]] already throws a TypeError.
> So it wouldn't affect necessarily all sites, especially if you continue to
> check for [[Call]] in `Array.prototype.forEach` (like what is currently
> done), etc.
>
> But why (and how) do you need “special-casing `typeof` and friends”? (The
>> way the question is formulated, avoiding special-casing would be an
>> argument for the current design. But I miss what are the special cases you
>> have in mind, especially regarding `typeof`.)
>>
>
> I was specifically referring to `typeof (class {}) === "function"`. The
> "and friends" was in reference to things like the callback in
> `Array.prototype.forEach`, which IIUC doesn't currently throw for classes
> if the array has no members.
>
> Sorry for the poor phrasing there.
>
>
>> —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/20170119/f06cabe8/attachment-0001.html>


More information about the es-discuss mailing list