Object.prototype.toSource()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/toSource
  • Revision title: Object.toSource
  • Revision id: 308887
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment -Kuma error, title(toSource > Object.toSource), +tag(JavaScript), See also section(+link+non-standard_inline) ... and more.

Revision Content

{{ Non-standard_header() }}

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

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

Revision Source

<div>{{ Non-standard_header() }}</div>



<h2 id="Summary" name="Summary">Summary</h2>

<p>Returns a string representing the source code of the object.</p>

<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.3</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>None</td>
    </tr>
  </tbody>
</table>



<h2 id="Syntax" name="Syntax">Syntax</h2>
<p><code><em>obj</em>.toSource()</code></p>



<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="eval">
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>

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


<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="See_Also" name="See_Also">See Also</h2>
<ul>
<li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Boolean/toSource" title="JavaScript/Reference/Global_Objects/Boolean/toSource">Boolean.toSource</a> {{Non-standard_inline()}}</li>
<li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object/toString" title="JavaScript/Reference/Global_Objects/Object/toString">Object.toString</a></li>
</ul>


<!-- languages({
"fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Objets_globaux/Object/toSource",
"ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/toSource",
"pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/toSource"}) -->
Revert to this revision