Table of contents
- 1. Summary
- 2. Syntax
- 3. Parameters
- 4. Description
- 5. Examples
- 6. See Also
Summary
Returns a string representing the source code of the object.
Method of Object | |
|---|---|
| Implemented in | JavaScript 1.3 |
| ECMAScript Edition | None |
Syntax
obj.toSource()
Parameters
None.
Description
The toSource method returns the following values:
- For the built-in
Objectobject,toSourcereturns the following string indicating that the source code is not available:
function Object() {
[native code]
}
- For instances of
Object,toSourcereturns a string representing the source code.
You can call toSource while debugging to examine the contents of an object.
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) + ")";
};
alert(new Person("Joe").toSource()); // ---> new Person("Joe")
Built-in toSource methods
Each core JavaScript type has its own toSource method. These objects are:
Examples
Example: Using 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", "girl");
Calling the toSource method of theDog displays the JavaScript source that defines the object:
theDog.toSource();
returns
({name:"Gabby", breed:"Lab", color:"chocolate", sex:"girl"})