过时
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

toSource() method returns a string representing the source code of the object. This method is usually called internally by JavaScript and not explicitly in code. You can call toSource() while debugging to examine the contents of an object.

句法

function.toSource();
					

返回值

A string representing the source code of the object.

范例

Native functions

For the built-in Function 对象, toSource() returns the following string indicating that the source code is not available:

function Function() {
  [native code]
}
					

Custom functions

For custom functions, toSource() returns the JavaScript source that defines the object as a string.

// For example:
function hello() {
  console.log("Hello, World!");
}
hello.toSource();
					
// Results in:
"function hello() {
    console.log(\"Hello, World!\");
}"
					

规范

Not part of any standard.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
toSource 非标 Chrome No Edge No Firefox 1 — 74
1 — 74
Starting in Firefox 74, toSource() is no longer available for use by web content. It is still allowed for internal and privileged code.
IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 4 Opera Android No Safari iOS No Samsung Internet Android No nodejs No

图例

完整支持

完整支持

不支持

不支持

非标。预期跨浏览器支持较差。

见实现注意事项。

另请参阅

元数据

  • 最后修改: