Il metodo toString () della funzione JavaScript restituisce il codice sorgente della funzione come stringa.
La sintassi del toString()
metodo è:
func.toString()
Ecco func
una funzione.
parametri toString ()
Il toString()
metodo non accetta parametri.
Valore restituito da toString ()
- Restituisce una stringa che rappresenta il codice sorgente della funzione.
Esempio: utilizzo di toString ()
function f() () console.log(f.toString()); // function f() () function sum(a, b) ( return a + b; ) console.log(sum.toString()); // function sum(a, b) ( // return a + b; // ) // built-in functions console.log(console.log); // log() ( (native code) ) console.log(Function.prototype.toString); // toString() ( (native code) ) console.log((() => "Hello World!").toString()); // () => "Hello World!"
Produzione
function f () () function sum (a, b) (return a + b;) log () ((native code)) toString () ((native code)) () => "Hello World!"
Lettura consigliata: JavaScript Object toString ()