Object.prototype.constructor

  • Revision slug: JavaScript/Reference/Global_Objects/Object/constructor
  • Revision title: constructor
  • Revision id: 375029
  • Created:
  • Creator: Dev
  • Is current revision? No
  • Comment

Revision Content

Summary

Returns a reference to the Object function that created the instance's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name, but it isn't read only (except for primitive Boolean, Number or String values: 1, true, "read-only").

Property of Object
Implemented in JavaScript 1.1
ECMAScript Edition ECMAScript 1st Edition

Description

All objects inherit a constructor property from their prototype:

var o = new Object // or o = {} in JavaScript 1.2
o.constructor == Object
var a = new Array // or a = [] in JavaScript 1.2
a.constructor == Array
var n = new Number(3)
n.constructor == Number

Even though you cannot construct most HTML objects, you can do comparisons. For example,

document.constructor == Document;
document.form3.constructor == Form;

Examples

Example: Displaying the constructor of an object

The following example creates a prototype, Tree, and an object of that type, theTree. The example then displays the constructor property for the object theTree.

function Tree(name) {
   this.name = name;
}

var theTree = new Tree("Redwood");
console.log( "theTree.constructor is " + theTree.constructor );

This example displays the following output:

theTree.constructor is function Tree(name) {
    this.name = name;
}

Example: Changing the constructor of an object

The following example shows how to modify constructor value of generic objects. Only true, 1 and "test" variable constructors will not be changed. This example explains that is not always so safe to believe in constructor function.

function Type() { }

var	types = [
	new Array,	[],
	new Boolean,	true,
	new Date,
	new Error,
	new Function,	function(){},
	Math,	
	new Number,	1,
	new Object,	{},
	new RegExp,	/(?:)/,
	new String,	"test"
];

for(var i = 0; i < types.length; i++) {
	types[i].constructor = Type;
	types[i] = [ types[i].constructor, types[i] instanceof Type, types[i].toString() ];
}

alert( types.join("\n") );

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a reference to the <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object" title="JavaScript/Reference/Global_Objects/Object">Object</a> function that created the instance's prototype. Note that the value of this property is a reference to the function itself, not a string containing the function's name, but it isn't read only (except for primitive Boolean, Number or String values: 1, true, "read-only").</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Property of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.1</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMAScript 1st Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Description" name="Description">Description</h2>
<p>All objects inherit a <code>constructor</code> property from their <code>prototype</code>:</p>
<pre class="brush:js">
var o = new Object // or o = {} in JavaScript 1.2
o.constructor == Object
var a = new Array // or a = [] in JavaScript 1.2
a.constructor == Array
var n = new Number(3)
n.constructor == Number</pre>
<p>Even though you cannot construct most HTML objects, you can do comparisons. For example,</p>
<pre class="brush:js">
document.constructor == Document;
document.form3.constructor == Form;</pre>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Displaying_the_constructor_of_an_object" name="Example:_Displaying_the_constructor_of_an_object">Example: Displaying the constructor of an object</h3>
<p>The following example creates a prototype, <code>Tree</code>, and an object of that type, <code>theTree</code>. The example then displays the <code>constructor</code> property for the object <code>theTree</code>.</p>
<pre class="brush:js">
function Tree(name) {
   this.name = name;
}

var theTree = new Tree("Redwood");
console.log( "theTree.constructor is " + theTree.constructor );</pre>
<p>This example displays the following output:</p>
<pre class="brush:js">
theTree.constructor is function Tree(name) {
    this.name = name;
}</pre>
<h3 id="Example:_Changing_the_constructor_of_an_object" name="Example:_Changing_the_constructor_of_an_object">Example: Changing the constructor of an object</h3>
<p>The following example shows how to modify constructor value of generic objects. Only true, 1 and "test" variable constructors will not be changed. This example explains that is not always so safe to believe in constructor function.</p>
<pre class="brush:js">
function Type() { }

var	types = [
	new Array,	[],
	new Boolean,	true,
	new Date,
	new Error,
	new Function,	function(){},
	Math,	
	new Number,	1,
	new Object,	{},
	new RegExp,	/(?:)/,
	new String,	"test"
];

for(var i = 0; i &lt; types.length; i++) {
	types[i].constructor = Type;
	types[i] = [ types[i].constructor, types[i] instanceof Type, types[i].toString() ];
}

alert( types.join("\n") );</pre>
Revert to this revision