过时
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:
Object
对象,
toSource()
returns the following string indicating that the source code is not available:
function Object() {
[native code]
}
Object
,
toSource()
returns a string representing the source code.
可以调用
toSource()
while debugging to examine the contents of an object.
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")
toSource()
methods
Each core JavaScript type has its own
toSource()
method. These objects are:
Array.prototype.toSource()
—
Array
对象。
Boolean.prototype.toSource()
—
布尔
对象。
Date.prototype.toSource()
—
Date
对象。
Function.prototype.toSource()
—
Function
对象。
Number.prototype.toSource()
—
Number
对象。
RegExp.prototype.toSource()
—
RegExp
对象。
String.prototype.toSource()
—
String
对象。
Symbol.prototype.toSource()
—
Symbol
对象。
Math.toSource()
— Returns the String "Math".
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.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toSource
弃用
非标
|
Chrome No | Edge No |
Firefox
1 — 74
|
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 |
完整支持
不支持
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见实现注意事项。
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function