37

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 ], which does not give me any information.

How can I get the signature of the function?

5
  • 1
    There are no types in javascript, so there is not much of a signature. Commented Sep 26, 2013 at 7:41
  • @Amberlamps Can i know the number of variables this function has, in its signature? Although we can pass as many as we want. Commented Sep 26, 2013 at 7:46
  • You can not read the signature ootb because of the missing types but I think this SO-answer will help you to built a helper function to detect the signature. Commented Sep 26, 2013 at 7:46
  • @AshishNegi Are you using Node.js? See the documentation of fs.readFile: nodejs.org/api/fs.html#fs_fs_readfile_filename_options_callback Commented Sep 26, 2013 at 7:46
  • 1
    functionname.toString().match(/function\s*(.*?)\s*{/)[1] Commented Sep 26, 2013 at 7:57

3 Answers 3

65

Call .toString() on the function:

$ node
> foo = function(bar, baz) { /* some code */ }
[Function: foo]
> foo
[Function: foo]
> foo.toString()
'function(bar, baz) { /* some code */ }'

or use a shortcut like foo+"".

Sign up to request clarification or add additional context in comments.

Comments

9

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.

Comments

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/

1 Comment

You're right, code is available after calling toString() of the function object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.