Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Print Function Signature in javascript

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

That does not give me any information.

How can I get the signature of the function?

like image 500
Ashish Negi Avatar asked Sep 26 '13 07:09

Ashish Negi


People also ask

How do you print a function in JavaScript?

JavaScript does not have any print object or print methods. You cannot access output devices from JavaScript. The only exception is that you can call the window.print() method in the browser to print the content of the current window.

What is function signature in JavaScript?

A function signature (or type signature, or method signature) defines input and output of functions or methods. A signature can include: parameters and their types. a return value and type. exceptions that might be thrown or passed back.

What is function signature in typescript?

Today I will describe how to write type signatures for a wide range of javascript functions. This will help readers narrow types, increase reliability, type dependencies, and more. A type signature is like a blueprint for any given segment of code.


2 Answers

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

like image 112
georg Avatar answered Sep 20 '22 04:09

georg


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.

like image 43
Andreas Hultgren Avatar answered Sep 19 '22 04:09

Andreas Hultgren