toSource

Non-standard

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 Object 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.

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"})

See Also

toString

Tags (0)

Edit tags
  • No tags

Attachments (0)

 

Attach file