In what Javascript engines does Function.prototype.toString not return the source code of that function?

混江龙づ霸主 提交于 2019-11-30 06:47:10

What current Javascript engines don't behave this way?

Your question isn't really well-defined, given that you haven't defined "popular". Is IE6 popular? IE5? IE4? Netscape Navigator? Lynx? The only way to properly answer your question is to enumerate which browsers you wish to support and check them. Unfortunately kangax's table http://kangax.github.io/es5-compat-table/# doesn't test Function.prototype.toString

Chrome's console will even tell you (when you pass anything other than a function o Function.toString.call), that Function.prototype.toString is not generic

mandated in the spec

the spec refrains from making any commitment as to what the behaviour should be when applied to functions

The required behavior is specified in ECMA-262 version 1 (from 1997, http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf) You have to chase it down:

From that, we deduce that functions are objects.

So now what is ToPrimitive?

So we need to know what DefaultValue does

Now we just need to find where Function.prototype.toString is described:

  • http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.4.2 "An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration. Note in particular that the use and placement of white space, line terminators, and semicolons within the representation String is implementation-dependent."

So you are guaranteed that you get a proper javascript representation (not some IL gobbledegook) but not necessarily with the comments. For example, the technique breaks in Firefox 16 (but then you have to ask if it is current).

So Kangax has returned to the subject matter (intrigued as he was by the fact that Angular uses this hack for core functionality in client-side code) and written up an analysis of the practice, and produced a test table for the state of function decompilation in Javascript.

The takeaway points are that:

  • The technique is only remotely reliable for user-defined function declarations.
  • Some old mobile browsers will still collapse functional code, allegedly for performance reasons.
  • Other old browsers will reveal optimized code, something like what you might get out of Closure Compiler.
  • Yet others will remove comments and alter whitespace.
  • Internet Explorer will occasionally add comments and whitespace around the functions.
  • The AngularJS team seem to think this technique is robust enough to include in their library without explicit caveat. They then tokenize (!) the code and re-evaluate it (!!).

For my purposes, this makes me reasonably confident I can do something relatively undemanding like detect whether a function has an uppercase name or not by parsing it as follows:

/function\s*[A-Z]/.test( fn )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!