如何在javascript中打印函数签名

我有一个function:

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

有一段时间后,我想在debugging过程中看到函数的签名。

当我尝试console.log(fs.readFile)我得到[ FUNCTION ]

那不给我任何信息。

我怎样才能得到函数的签名?

特别是在node.js中,你必须在logging之前把函数转换成string:

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

或使用快捷方式如foo+""

我不知道你想要什么,但试着看这个小提琴的控制台日志,它打印整个function定义。 我正在看chrome console.log输出。

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

DEMO http://jsfiddle.net/K7DMA/

如果“函数签名”指的是它定义了多less个参数,则可以使用:

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

所有的函数自动获得一个长度属性。