Calling toString on function proxy throws TypeError exception

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Oct 27 17:41:52 UTC 2015


> On Oct 27, 2015, at 2:37 AM, Claude Pache <claude.pache at gmail.com> wrote:
> 
> 
> Naturally, I meant:
> 
> ```js
> Function.isGenerator = function (f) {
>   return typeof f == "function" && f.toString().match(/^function\s*\*/) != null
> }
> ```
> 
> —Claude
> 
>> 
>> That function will work (in the sense of: will return an answer; I'm not judging the quality of that answer) with anything reasonable fed to it (where "reasonable" excludes things like `(class { static toString() { throw "pwnd!" }})`).  Well, ... until a proxy for a function is encountered.
>> 
>> —Claude

This may have been pragmatically useful but it has never been a reliable test because there was nothing that stops somebody from adding an arbiietrary `toString` own property to f or from using dunder photo to change the prototype used to supply `toString`.

Also, going back to at lest ES3, F.p.toString was specified to throw a TypeError when applied to an object  that “is not Function object”.

That said, I think it is plausible that the predicate in step 2 of http://tc39.github.io/ecma262/#sec-function.prototype.tostring <http://tc39.github.io/ecma262/#sec-function.prototype.tostring> could be replaced with: IsCallable(func)

Allen 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151027/2739790b/attachment-0001.html>


More information about the es-discuss mailing list