1

This may be a very dumb question. I want to see the code of a function (Built in and User defined) in Javascript.

For example :
function hello(){
console.log("hello")
}

hello.toString() // Gives the function definition
'function hello(){\nconsole.log("hello")\n}'

Is there a way to see the native code like Math.random.toString()?

Update: From the comments, Seblor explained that native code cannot be seen.

Pavan Skipo
  • 1,757
  • 12
  • 29

1 Answers1

1

You could do some string formatting to get a "better" look at your functions. Use this peace of code to get rid of the function name to get just the code.

function justGetCode(funcName)
{
    var tempString = funcName.toString();
    tempString = tempString.substring(tempString.indexOf("{"));

    return tempString

}

But beyond this there is little you can do in terms of digging into native (i.e. browser specific ) code as it is encapsulated. This should work on library functions however.

Now i do not know what you are planning on doing with the returned function, but for fancier function manipulation you can always use in-built reflection mechanisms