EN
JavaScript - get function source code
8 points
In this article, we would like to show how to get source code for a given function in JavaScript.
Quick solution (use toString()
on function):
xxxxxxxxxx
1
const myFunction = () => {
2
console.log('myFunction called...');
3
};
4
5
console.log(myFunction.toString());
Note: be careful using transpilers or minifiers that change function source code - code is optimized.
xxxxxxxxxx
1
function myFunction1() {
2
console.log('myFunction1 called...');
3
}
4
5
const myFunction2 = function FunctionName() {
6
console.log('FunctionName called...');
7
};
8
9
const myFunction3 = function() {
10
console.log('myFunction3 called...');
11
};
12
13
const myFunction4 = () => {
14
console.log('myFunction3 called...');
15
};
16
17
// Usage example:
18
19
console.log(myFunction1.toString());
20
console.log(myFunction2.toString());
21
console.log(myFunction3.toString());
22
console.log(myFunction4.toString());