javascript 如何在javascript中打印函数签名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19022121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:06:00  来源:igfitidea点击:

How to Print Function Signature in javascript

javascriptnode.jsdebugging

提问by Ashish Negi

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 ].

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

That does not give me any information.

那没有给我任何信息。

How can I get the signature of the function?

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

回答by georg

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

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

$ 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+""

或使用快捷方式 foo+""

回答by Andreas Hultgren

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.

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

回答by ???? ???p

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.

我不确定你想要什么,但尝试查看这个小提琴的控制台日志,它打印了整个函数定义。我正在查看 chrome console.log 输出。

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

DEMO http://jsfiddle.net/K7DMA/

演示 http://jsfiddle.net/K7DMA/