在 javascript 函数上调用 toString 返回源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5327668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:41:27  来源:igfitidea点击:

Calling toString on a javascript function returns source code

javascripttostring

提问by Sergi Papaseit

I just found out that when you call toString() on a javascript function, as in myFunction.toString(), the source code of that function is returned.

我刚刚发现,当您在 javascript 函数上调用 toString() 时,如在 中myFunction.toString(),将返回该函数的源代码。

If you try it in the Firebug or Chrome console it will even go as far as formatting it nicely for you, even for minimized javascript files.
I don't know what is does for obfuscated files.

如果您在 Firebug 或 Chrome 控制台中尝试它,它甚至可以为您很好地格式化它,即使是最小化的 javascript 文件。
我不知道混淆文件有什么作用。

What's the use of such a toString implementation?

这样的 toString 实现有什么用?

采纳答案by HoLyVieR

It has some use for debugging, since it lets you see the code of the function. You can check if a function has been overwritten, and if a variable points to the right function.

它对调试有一些用处,因为它可以让您看到函数的代码。您可以检查函数是否已被覆盖,以及变量是否指向正确的函数。

It has some uses for obfuscated javascript code. If you want to do hardcore obfuscation in javascript, you can transform your whole code into a bunch of special characters, and leave no numbers or letters. This technique relies heavily on being able to access most letters of the alphabet by forcing the toString call on everything with +""(example : (![]+"")[+[]]is f). Some letters like vcan only be accessed by calling toString on a native function like [].sort. The letter vis important for obfuscated code, since it lets you call eval, which lets you execute anything, even loops, without using any letters. Here is an example of this.

它对混淆的 javascript 代码有一些用途。如果您想在 javascript 中进行核心混淆,您可以将整个代码转换为一堆特殊字符,并且不留下数字或字母。这种技术在很大程度上依赖于能够通过强制对所有内容进行 toString 调用来访问字母表中的大多数字母+""(例如 : (![]+"")[+[]]is f)。某些字母v只能通过在本机函数上调用 toString 来访问,例如[].sort. 这个字母v对于混淆代码很重要,因为它可以让你调用eval,它可以让你在不使用任何字母的情况下执行任何事情,甚至是循环。这是一个例子

回答by CloudyMarble

function.ToString - Returns a string representing the source code of the function. For Function objects, the built-in toString method decompiles the function back into the JavaScript source that defines the function.

function.ToString - 返回表示函数源代码的字符串。对于 Function 对象,内置的 toString 方法将函数反编译回定义该函数的 JavaScript 源代码。

Read thison mozilla.

在 mozilla 上阅读这篇文章。

回答by Barney

You can use it as an implementation for multi-line strings in Javascript source.

您可以将其用作 Javascript 源代码中多行字符串的实现。

As described in this blog post by @tjanczuk, one of the massive inconveniences in Javascript is multi-line strings. But you can leverage .toString()and the syntax for multi-line comments (/* ... */) to produce the same results.

正如@tjanczuk这篇博文中所描述的,Javascript 的一大不便之处是多行字符串。但是您可以利用.toString()和 多行注释 ( /* ... */)的语法来产生相同的结果。

By using the following function:

通过使用以下功能

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

…you can then pass in multi-line comments in the following format:

...然后您可以按以下格式传递多行注释:

var superString = uncomment(function(){/*
String line 1
String line 2
String line 3
*/});

In the original article, it was noted that Function.toString()'s behaviour is not standardised and therefore implementation-discrete — and the recommended usage was for Node.js (where the V8 interpreter can be relied on); however, a Fiddle I wrote seems to work on every browser I have available to me(Chrome 27, Firefox 21, Opera 12, Internet Explorer 8).

在最初的文章中,指出Function.toString()的行为不是标准化的,因此是实现离散的——推荐的用法是用于 Node.js(可以依赖 V8 解释器);但是,我编写的 Fiddle 似乎适用于我可用的所有浏览器(Chrome 27、Firefox 21、Opera 12、Internet Explorer 8)。

回答by R.Moeller

A nice use case is remoting. Just toString the function in the client, send it over the wire and execute it on the server.

一个很好的用例是远程处理。只需将客户端中的函数字符串化,通过网络发送它并在服务器上执行它。

回答by Ken Lin

My use case - I have a node program that processes data and produces interactive reports as html/js/css files. To generate a js function, my node code calls myfunc.toString() and writes it to a file.

我的用例 - 我有一个节点程序,它处理数据并生成交互式报告作为 html/js/css 文件。为了生成 js 函数,我的节点代码调用 myfunc.toString() 并将其写入文件。

回答by Kpym

You can use it to create a Web Worker from function defined in the main script:

您可以使用它从主脚本中定义的函数创建一个 Web Worker:

onmessage = function(e) {
  console.log('[Worker] Message received from main script:',e.data);
  postMessage('Worker speaking.');
}

b = new Blob(["onmessage = " + onmessage.toString()], {type: 'text/javascript'})

w = new Worker(window.URL.createObjectURL(b));

w.onmessage = function(e) {
    console.log('[Main] Message received from worker script:' + e.data);
  };

w.postMessage('Main speaking.');