依赖 Function.prototype.toString
返回一个将解析为有效 JavaScript 函数(对于用户定义函数)的字符串是否安全?
是否有任何常用的 JavaScript 引擎在如何以字符串形式表示函数对象方面偏离了规范?
我看过这个问题,但我不确定它是否是问同样的事情。我不关心所有实现中的格式是否完全相同或其他什么,我更担心一些缩小的 js 引擎只是剥离整个函数体...
另一个 相关问题,但不够密切相关,不足以为该问题提供令人满意的答案。
Is it safe to rely on Function.prototype.toString
to return a string that will parse as a valid javascript function (for user-defined functions)?
Are there any commonly-used javascript engines that deviate from the norm as far as how they represent function objects in string form?
I have seen this question, but I'm not sure if it's asking the same thing. I don't care if the formatting is exactly the same in all implementations or whatever, I'm more worried about some minified js engine just stripping out the whole function body...
Another related question, but not closely related enough to have a satisfying answer for this question.
发布评论
评论(2)
我认为它是安全的,因为它是一个标准。每一个严肃的引擎都可以。这也是我的项目 Jscex 的基础。它适用于所有浏览器(甚至是旧版 IE6)和 Node.js。我做了这样的事情很多年了。 :)
I think it's safe since it's a standard. Every serious engine would do. That's also what my project Jscex is based on. It works for all the browsers (even the legacy IE6) and Node.js. I do this kind of things for year. :)
应该注意的是,eval'd 代码将采用当前作用域,而 Function 构造函数将仅采用全局作用域。
It should be noted that eval'd code will take on the current scope and the Function constructor will only take on the global scope.