Core JavaScript 1.5 Reference:Global Objects:Object:toSource
From MDC
Non-standard
Contents |
[edit] Summary
Returns a string representing the source code of the object.
| Method of Object | |
| Implemented in: | JavaScript 1.3 |
[edit] Syntax
obj.toSource()
[edit] Parameters
None.
[edit] 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.
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.
[edit] Built-in toString methods
Each core JavaScript object of a unique class that also provides its own prototype object has a unique toString method. The purpose of this method is to provide an appropriate value when JavaScript needs to convert that object into a string. These objects are:
- toSource - Array Object method.
- toSource - Boolean Object method.
- toSource - Date Object method.
- toSource - Function Object method.
- toSource - Number Object method.
- toSource - Object Object method.
- toSource - RegExp Object method.
- toSource - String Object method.
[edit] Examples
[edit] 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"})