Metoda JavaScript Function toString () zwraca kod źródłowy funkcji jako ciąg znaków.
Składnia toString()
metody to:
func.toString()
Tutaj func
jest funkcja.
toString () Parametry
toString()
Metoda nie ma żadnych parametrów.
Wartość zwracana z toString ()
- Zwraca ciąg znaków reprezentujący kod źródłowy funkcji.
Przykład: użycie 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!"
Wynik
function f () () function sum (a, b) (return a + b;) log () ((kod natywny)) toString () ((kod natywny)) () => "Witaj świecie!"
Zalecana lektura: Obiekt JavaScript toString ()