JavaScript函数toString()
JavaScript Function toString()方法以字符串返回函数的源代码。
toString()
方法的语法为:
func.toString()
在这里, func
是一个函数 。
toString()参数
toString()
方法没有任何参数。
从toString()返回值
- 返回表示该函数源代码的字符串 。
示例:使用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!"
输出
function f() {} function sum(a, b) { return a + b; } log() { [native code] } toString() { [native code] } () => "Hello World!"
推荐阅读: JavaScript对象toString()
下一篇: JavaScript数学abs()
总计 0 评论