Core JavaScript 1.5 Reference:Global Objects:Function:toString
From MDC
Contents |
[edit] Summary
Returns a string representing the source code of the function.
| Method of Function | |
| Implemented in: | JavaScript 1.1, NES 2.0 |
| ECMA Version: | ECMA-262 |
[edit] Syntax
object.toString()
[edit] Parameters
None.
[edit] Description
The Function object overrides the toString method of the Object object; it does not inherit Object.toString. For Function objects, the toString method returns a string representation of the object.
JavaScript calls the toString method automatically when a Function is to be represented as a text value or when a Function is referred to in a string concatenation.
For Function objects, the built-in toString method decompiles the function back into the JavaScript source that defines the function. This string includes the function keyword, the argument list, curly braces, and function body.
For example, assume you have the following code that 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");
Any time Dog is used in a string context, JavaScript automatically calls the toString function, which returns the following string:
function Dog(name, breed, color, sex) {
this.name = name;
this.breed = breed;
this.color = color;
this.sex = sex;
}