JavaScript 函数方法 - toString()

时间:2023-03-08 23:36:36
JavaScript 函数方法 - toString()

Function.prototype.toString()

  返回函数代码的字符串形式。

描述

  Function 对象覆盖了从 Object 继承来的 Object.prototype.toString 方法。函数的 toString 方法会返回一个表示函数源代码的字符串。具体来说,包括 function关键字,形参列表,大括号,以及函数体中的内容。

案例: 一般情况下,函数在需要字符串形式情况下会自动调用toString()方法

  

function autoFormat(){
console.log("自动转换");
} autoFormat + "自动调用" // "function autoFormat(){console.log("自动转换");}自动调用"