如何在javascript中打印功能签名

Ash*_*egi 17 javascript debugging node.js

我有一个功能:

fs.readFile = function(filename, callback) {
    // implementation code.
};
Run Code Online (Sandbox Code Playgroud)

一段时间后,我想在调试过程中看到函数的签名.

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

那不给我任何信息.

如何获得该功能的签名?

geo*_*org 32

在node.js中,您必须在记录之前将函数转换为字符串:

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

或使用像.的快捷方式 foo+""


And*_*ren 5

如果"函数签名"的含义是它定义了多少个参数,您可以使用:

function fn (one) {}
console.log(fn.length); // 1
Run Code Online (Sandbox Code Playgroud)

所有函数都自动获取长度属性.