过时
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.

句法

Object.toSource();
obj.toSource();

					

返回值

A string representing the source code of the object.

描述

toSource() method returns the following values:

  • For the built-in Object 对象, toSource() returns the following string indicating that the source code is not available:
    function Object() {
        [native code]
    }
    							
  • For instances of Object , toSource() returns a string representing the source code.

可以调用 toSource() while debugging to examine the contents of an object.

Overriding the toSource() 方法

It is safe for objects to override the toSource() method. For example:

function Person(name) {
  this.name = name;
}
Person.prototype.toSource = function Person_toSource() {
  return 'new Person(' + uneval(this.name) + ')';
};
console.log(new Person('Joe').toSource()); // ---> new Person("Joe")
					

Built-in toSource() methods

Each core JavaScript type has its own toSource() method. These objects are:

Limitations on cyclical objects

In the case of objects that contain references to themselves, e.g. a cyclically linked list or a tree that can be traversed both ways, toSource() will not recreate the self-reference, as of Firefox 24. For example:

var obj1 = {};
var obj2 = { a: obj1 };
obj1.b = obj2;
console.log('Cyclical: ' + (obj1.b.a == obj1));
var objSource = obj1.toSource(); // returns "({b:{a:{}}})"
obj1 = eval(objSource);
console.log('Cyclical: ' + (obj1.b.a == obj1));
					

If a cyclical structure is employed and toSource() is needed, the object must provide an override to toSource() , either using a reference to a constructor or providing an anonymous function.

范例

使用 toSource()

The following code defines the Dog object type and creates theDog , an object of type Dog :

function Dog(name, breed, color, sex) {
  this.name = name;
  this.breed = breed;
  this.color = color;
  this.sex = sex;
}
theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');
					

Calling the toSource() 方法为 theDog displays the JavaScript source that defines the object:

theDog.toSource();
// returns ({name:"Gabby", breed:"Lab", color:"chocolate", sex:"female"})
					

规范

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

图例

完整支持

完整支持

不支持

不支持

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

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

另请参阅

元数据

  • 最后修改: