Revision 493737 of Object.prototype.toSource()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/Object/toSource
  • Revision title: Object.prototype.toSource()
  • Revision id: 493737
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment cleanup

Revision Content

{{JSRef("Global_Objects", "Object")}} {{ Non-standard_header() }}

Summary

The toSource() method returns a string representing the source code of the object.

Syntax

Object.toSource();
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.

Overriding the toSource method

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: {{page("/en-US/docs/ToSource")}}

Limitations on cyclical objects

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;

alert("Cyclical: "+(obj1.b.a==obj1));

var objSource = obj1.toSource(); // returns "({b:{a:{}}})"

obj1 = eval(objSource);

alert("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.

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

Specifications

Not part of any standard. Implemented in JavaScript 1.3.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatUnknown() }} {{ CompatVersionUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatVersionUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

See also

  • {{jsxref("Object.prototype.toString()")}}

Revision Source

<div>
 {{JSRef("Global_Objects", "Object")}} {{ Non-standard_header() }}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>toSource()</strong></code> method returns a string representing the source code of the object.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
Object.toSource();
<code><em>obj</em>.toSource();</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<p>None.</p>
<h2 id="Description" name="Description">Description</h2>
<p>The <code>toSource</code> method returns the following values:</p>
<ul>
 <li>For the built-in <code>Object</code> object, <code>toSource</code> returns the following string indicating that the source code is not available:</li>
</ul>
<pre class="brush: js">
function Object() {
   [native code]
}</pre>
<ul>
 <li>For instances of <code>Object</code>, <code>toSource</code> returns a string representing the source code.</li>
</ul>
<p>You can call <code>toSource</code> while debugging to examine the contents of an object.</p>
<h3>Overriding the <code>toSource</code> method</h3>
<p>It is safe for objects to override the <code>toSource</code> method. For example:</p>
<pre class="brush:js">
function Person(name) {
    this.name = name;
}

Person.prototype.toSource = function Person_toSource() {
    return "new Person(" + uneval(this.name) + ")";
};

alert(new Person("Joe").toSource()); // ---&gt; new Person("Joe")</pre>
<h3 id="Built-in_toString_methods" name="Built-in_toString_methods">Built-in <code>toSource</code> methods</h3>
<p>Each core JavaScript type has its own <code>toSource</code> method. These objects are: {{page("/en-US/docs/ToSource")}}</p>
<h3 id="Limitations_on_cyclical_objects">Limitations on cyclical objects</h3>
<p>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,&nbsp;<code>toSource</code> will not recreate the self-reference, as of Firefox 24. For example:</p>
<pre>
var obj1 = {};
var obj2 = {a: obj1};
obj1.b = obj2;

alert("Cyclical: "+(obj1.b.a==obj1));

var objSource = obj1.toSource(); // returns "({b:{a:{}}})"

obj1 = eval(objSource);

alert("Cyclical: "+obj1.b.a==obj1));
</pre>
<p>If a cyclical structure is employed and toSource is needed, the object must provide an override to <code>toSource</code>, either using a reference to a constructor or providing an anonymous function.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_toSource" name="Example:_Using_toSource">Example: Using <code>toSource</code></h3>
<p>The following code defines the <code>Dog</code> object type and creates <code>theDog</code>, an object of type <code>Dog</code>:</p>
<pre class="brush:js">
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");</pre>
<p>Calling the <code>toSource</code> method of <code>theDog</code> displays the JavaScript source that defines the object:</p>
<pre class="brush:js">
theDog.toSource();</pre>
<p>returns</p>
<pre class="brush:js">
(\{name:"Gabby", breed:"Lab", color:"chocolate", sex:"girl"})</pre>
<h2 id="Specifications">Specifications</h2>
<p>Not part of any standard. Implemented in JavaScript 1.3.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatUnknown() }}</td>
    <td>{{ CompatUnknown() }}</td>
    <td>{{ CompatUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatUnknown() }}</td>
    <td>{{ CompatUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatUnknown() }}</td>
    <td>{{ CompatUnknown() }}</td>
    <td>{{ CompatUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_Also" name="See_Also">See also</h2>
<ul>
 <li>{{jsxref("Object.prototype.toString()")}}</li>
</ul>
Revert to this revision