Question: I got some JS Code that gets inside a random Anonymous js function.
I want that code (for example `alert('hello')` ) to dump/alert 
the entire script block/object which it was injected into. 

kinda like document.body.innerHTML but for the anonymous function block 

result should be like :

```
Function()({ somecode; MyAlert(...) } )()

```

or

```
Try { some code; mycode; } catch(e) { }

```

Answer: 
Functions have a `toString()` method. (Yes functions have methods!)

```
var fn = function() { alert('hello') };
fn.toString() // "function() { alert('hello') };"

```

So you can alert it:

```
alert(fn.toString());

```

You can log it to the js console:

```
console.log(fn.toString());

```

Or even write it to the page.

```
document.getElementById('someID').innerHTML = fn.toString();

```

---

However, this won't work for every function in the universe.

```
[].push.toString()
"function push() { [native code] }"

```

Some functions are not implemented with javascript, but in the compiled code of the browser or JS engine. For these environment provided functions, you will get this above less helpful output.
If you're **not** in strict mode you can go up the stack from something which was referenceable (i.e. a named function expression) using (non-standard) [`.caller`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller)

```
function getFunctionReference(callback) {
    var ref = getFunctionReference.caller;
    if (callback) callback(ref);
    return ref;
}

```

Now you can do things like

```
(function () {
    getFunctionReference(alert);
}());
// alerts the .toString of the IIFE

```

This only works for *functions*, you can't do this on top level code.

---

The best way to explore your code is actually with the *Console*, and you can use the [`debugger;`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) statement or breakpoints to follow exactly what is happening and when.