Revision 81296 of Object.prototype.toSource()

  • Revision slug: JavaScript/Reference/Global_Objects/Object/toSource
  • Revision title: toSource
  • Revision id: 81296
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Adding: [[fr:Référence de JavaScript 1.5 Core:Objets globaux:Object:toSource]] <<langbot>>

Revision Content

{{ Non-standard_header() }}

Summary

Returns a string representing the source code of the object.

Method of Object
Implemented in: JavaScript 1.3

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.

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.

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: {{ :en/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

toString

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

Revision Source

<p>
{{ Non-standard_header() }}
</p>
<h3 name="Summary"> Summary </h3>
<p>Returns a string representing the source code of the object.
</p>
<table class="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Method of <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.3</td>
</tr>
</tbody></table>
<h3 name="Syntax"> Syntax </h3>
<p><code><i>obj</i>.toSource()</code>
</p>
<h3 name="Parameters"> Parameters </h3>
<p>None.
</p>
<h3 name="Description"> Description </h3>
<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>This method is usually called internally by JavaScript and not explicitly in code. You can call <code>toSource</code> while debugging to examine the contents of an object.
</p>
<h4 name="Built-in_toString_methods"> Built-in <code>toString</code> methods </h4>
<p>Each core JavaScript object of a unique class that also provides its own prototype object has a unique <code>toString</code> 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: {{ :en/ToSource() }}
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_toSource"> Example: Using <code>toSource</code> </h4>
<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="eval">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="eval">theDog.toSource();
</pre>
<p>returns
</p>
<pre class="eval">({name:"Gabby", breed:"Lab", color:"chocolate", sex:"girl"})
</pre>
<h3 name="See_Also"> See Also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">toString</a>
</p>
<div class="noinclude">
</div>
{{ 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