简体   繁体   中英

Calling toString on a javascript function returns source code

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.

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.

What's the use of such a toString implementation?

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 since (![]+"") evaluates to the string "false" and [+[]] evaluates to [0] , thus you get "false"[0] which extracts the first letter f .

Some letters like v can only be accessed by calling toString on a native function like [].sort . The letter v is 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 .

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.

Read this on mozilla.

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

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.

By using thefollowing 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).

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM