JavaScript参考手册
函数 | Function
function.toString
该 toString()
方法返回一个表示当前函数源代码的字符串。
语法
function.toString()
返回值
表示函数源代码的字符串。
描述
Function
对象覆盖了从 Object
继承来的 Object.prototype.toString 方法。
函数的 toString 方法
会返回一个表示函数源代码的字符串。具体来说,包括 function
关键字,形参列表,大括号,以及函数体中的内容。
在函数需要转换为字符串时,通常会自动调用函数的 toString 方法。
如果它的值对象不是一个函数对象,该toString()
方法将抛出一个TypeError
异常(“Function.prototype.toString called on incompatible object”)。例如,它也会抛出thisFunctionProxy
对象。
Function.prototype.toString.call('foo'); // TypeError
var proxy = new Proxy(function() {}, {});
Function.prototype.toString.call(proxy); // TypeError
如果toString()
在ECMAScript代码中未定义的内置函数对象上调用方法,则toString
返回一个本地函数字符串,如下所示:
Math.abs.toString();
"function abs() {
[native code]
}"
规范
Specification |
Status |
Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) |
Standard |
Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Function.prototype.toString' in that specification. |
Standard |
Added more specific requirements for the string representation. |
Function.prototype.toString revision |
Draft |
Standardizes native function string, line endings. |
ECMAScript Latest Draft (ECMA-262)The definition of 'Function.prototype.toString' in that specification. |
Living Standard |
|
浏览器兼容性
Feature |
Chrome |
Edge |
Firefox (Gecko) |
Internet Explorer |
Opera |
Safari |
---|---|---|---|---|---|---|
Basic support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
Revised support |
? |
? |
54.0 (54.0) |
? |
? |
? |
Feature |
Android |
Chrome for Android |
Edge |
Firefox Mobile (Gecko) |
IE Mobile |
Opera Mobile |
Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
Revised support |
? |
? |
? |
54.0 (54.0) |
? |
? |
? |
函数 | Function相关
JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络