Calling toString on a javascript function returns source code
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.