Gupta
Gupta

Reputation: 150

Get source of a function by name in Javascript

Is there a way to get the source code of a function by using it's name in javacsript? I'm using the console and while like to see the source code of the function but can't find it. I can execute it in the console and typing only in the name returns the function object.

Upvotes: 0

Views: 335

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 370779

If you log it with console.dir, at least in Chrome, you'll see an internal [[FunctionLocation]] property that's clickable. For example, Stack Exchange uses jQuery, so doing console.dir($) in the console shows:

enter image description here

Upvotes: 3

Joseph
Joseph

Reputation: 6259

you could achieve that by printing the function name only without invoke it

function sum(x, y){
   return x + y;
}

console.log(sum)

Upvotes: 2

Related Questions