How to Print Function Signature in javascript

后端 未结 3 1953
再見小時候
再見小時候 2021-02-02 10:08

I have a function:

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

Sometime later I want to see the signature of th

相关标签:
3条回答
  • 2021-02-02 10:26

    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/

    0 讨论(0)
  • 2021-02-02 10:27

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

    0 讨论(0)
  • 2021-02-02 10:38

    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.

    0 讨论(0)
提交回复
热议问题