首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

function.toString

该?toString()方法返回一个表示当前函数源代码的字符串。

语法

代码语言:javascript
复制
function.toString()

返回值

表示函数源代码的字符串。

描述

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

在函数需要转换为字符串时,通常会自动调用函数的?toString 方法。

如果它的值对象不是一个函数对象,该toString()方法将抛出一个TypeError异常(“Function.prototype.toString called on incompatible object”)。例如,它也会抛出thisFunctionProxy对象。

代码语言:javascript
复制
Function.prototype.toString.call('foo'); // TypeError

var proxy = new Proxy(function() {}, {});
Function.prototype.toString.call(proxy); // TypeError

如果toString()在ECMAScript代码中未定义的内置函数对象上调用方法,则toString返回一个本地函数字符串,如下所示:

代码语言:javascript
复制
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)

?

?

?

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com