How to Print Function Signature in javascript

22,137

Solution 1

In node.js specifically, you have to convert the function to string before logging:

$ node
> foo = function(bar, baz) { /* codez */ }
[Function]
> console.log(foo)
[Function]
undefined
> console.log(foo.toString())
function (bar, baz) { /* codez */ }
undefined
> 

or use a shortcut like foo+""

Solution 2

If what you mean by "function signature" is how many arguments it has defined, you can use:

function fn (one) {}
console.log(fn.length); // 1

All functions get a length property automatically.

Solution 3

I am not sure what you want but try looking at the console log of this fiddle, it prints entire function definition. I am looking at chrome console.log output.

var fs = fs || {};
fs.readFile = function(filename, callback) {
  alert(1);
};
console.log(fs.readFile);

DEMO http://jsfiddle.net/K7DMA/

Share:
22,137
Ashish Negi
Author by

Ashish Negi

i am here to learn

Updated on September 26, 2020

Comments

  • Ashish Negi
    Ashish Negi over 3 years

    I have a function:

    fs.readFile = function(filename, callback) {
        // implementation code.
    };
    

    Sometime later I want to see the signature of the function during debugging.

    When I tried console.log(fs.readFile) I get [ FUNCTION ].

    That does not give me any information.

    How can I get the signature of the function?