Find JavaScript function definition in Chrome
Lets say we're looking for function named foo
:
- (open Chrome dev-tools),
- Windows: ctrl + shift + F, or macOS:cmd + optn + F. This opens a window for searching across all scripts.
- check "Regular expression" checkbox,
- search for
foo\s*=\s*function
(searches forfoo = function
with any number of spaces between those three tokens), - press on a returned result.
Another variant for function definition is function\s*foo\s*\(
for function foo(
with any number of spaces between those three tokens.
This landed in Chrome on 2012-08-26Not sure about the exact version, I noticed it in Chrome 24.
A screenshot is worth a million words:
I am inspecting an object with methods in the Console. Clicking on the "Show function definition" takes me to the place in the source code where the function is defined. Or I can just hover over the function () {
word to see function body in a tooltip. You can easily inspect the whole prototype chain like this! CDT definitely rock!!!
Hope you all find it helpful!
You can print the function by evaluating the name of it in the console, like so
> unknownFuncfunction unknownFunc(unknown) { alert('unknown seems to be ' + unknown);}
this won't work for built-in functions, they will only display [native code]
instead of the source code.
EDIT: this implies that the function has been defined within the current scope.