在什么Javascript引擎中,Function.prototype.toString不返回该函数的源代码? [英] In what Javascript engines does Function.prototype.toString not return the source code of that function?

查看:104
本文介绍了在什么Javascript引擎中,Function.prototype.toString不返回该函数的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:明确地说,我不是在寻找有关功能所隐含的各种问题的定性价值的建议或意见 - 我也不是在寻找实际问题的可靠解决方案;我只是在寻找标题中问题的技术性,可验证的答案。我在问题中添加了一系列不符合标准的浏览器。

使用函数的 .toString 方法通常会呈现该功能的源代码。问题是这种行为未指定 - 规范禁止制作任何内容关于应用于函数时应该采取什么行为的承诺。 Chrome的控制台甚至会告诉你(当你将一个函数以外的任何东西传递给 Function.toString.call )时, Function.prototype.toString不是通用的

Using a function's .toString method will typically render the source code for that function. The problem is that this behaviour isn't specified — the spec refrains from making any commitment as to what the behaviour should be when applied to functions. Chrome's console will even tell you (when you pass anything other than a function to Function.toString.call), that Function.prototype.toString is not generic

这篇博客文章建议这可以用作为多行字符串生成可读语法的方法(通过将字符串存储为无操作功能)。作者在编写 Node.js 应用程序的上下文中提出了这种用法,该条款规定此行为仅可靠,因为Node.js在受控制的情况下运行环境。但是在Javascript的原生网络中,任何东西都可以出现并解释它,我们不应该依赖于未指定的行为。

This blog post suggests this can be used as a method to produce a readable syntax for multi-line strings (by storing the string as a multi-line comment in the body of a no-op function). The author suggests this usage in the context of writing Node.js applications with the clause that this behaviour is only reliable because Node.js runs in a controlled environment. But in Javascript's native web, anything can come along and interpret it, and we shouldn't rely on unspecified behaviour.

在实践中,我已经设置了一个小提琴,它呈现一个选择框,其内容由一个大型多行字符串确定,以测试代码,以及我工作站上的每个浏览器(Chrome 27,Firefox 21,Opera 12,Safari 5,Internet Explorer 8)按预期执行。

In practice though, I've set up a fiddle which renders a select box whose contents are determined by a large multi-line string to test the code, and every browser on my workstation (Chrome 27, Firefox 21, Opera 12, Safari 5, Internet Explorer 8) executes as intended.

鉴于:

function uncomment(fn){
  return fn.toString().split(/\/\*\n|\n\*\//g).slice(1,-1).join();
}

以下内容:

uncomment(function(){/*
erg
arg
*/});

应输出:

erg
arg



不合规浏览器列表:




  • Firefox 16

  • ...

  • 推荐答案


    目前哪些Javascript引擎不这样做?

    What current Javascript engines don't behave this way?

    鉴于你没有定义流行,你的问题并没有真正明确定义。 IE6很受欢迎吗? IE5? IE4? Netscape Navigator?山猫?正确回答问题的唯一方法是枚举您希望支持的浏览器并进行检查。不幸的是kangax的表 http://kangax.github.io/es5-compat-table/# 没有' t test Function.prototype.toString

    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的控制台甚至可以告诉你(当你传递函数以外的任何东西时)函数.toString.call ),那个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

    规范


    规范禁止对应用于函数时的行为做出任何承诺

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

    所需行为在ECMA-262第1版中规定(自1997年起, http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf )你必须追逐它:

    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:

    • http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.24 "function ... member of the Object type that is an instance of the standard built-in Function constructor and that may be invoked as a subroutine"

    由此,我们推断出函数是对象。

    From that, we deduce that functions are objects.

    • http://www.ecma-international.org/ecma-262/5.1/#sec-9.8 "Let primValue be ToPrimitive(input argument, hint String)."

    那么现在什么是ToPrimitive?

    So now what is ToPrimitive?

    • http://www.ecma-international.org/ecma-262/5.1/#sec-9.1 " The default value of an object is retrieved by calling the [[DefaultValue]] internal method of the object, passing the optional hint PreferredType. "

    所以我们需要知道DefaultValue的作用

    So we need to know what DefaultValue does

    • http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.8 (lots of words that basically say if the thing has a toString method, then call it)

    现在我们只需要找到描述Function.prototype.toString的位置:

    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."

    所以你保证你得到一个正确的javascript表示(不是一些IL gobbledegook),但不一定是注释。例如,这个技术在Firefox 16中断(但是你必须问它是否是最新的。)

    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).

    这篇关于在什么Javascript引擎中,Function.prototype.toString不返回该函数的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆