Object.prototype.constructor

  • Revision slug: JavaScript/Reference/Global_Objects/Object/constructor
  • Revision title: constructor
  • Revision id: 87962
  • Created:
  • Creator: Sevenspade
  • Is current revision? No
  • Comment /* Summary */

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, NES2.0
ECMA Version: ECMA-262

Description

All objects inherit a constructor property from their prototype:

o = new Object // or o = {} in JavaScript 1.2
o.constructor == Object
a = new Array // or a = [] in JavaScript 1.2
a.constructor == Array
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;
}
theTree = new Tree("Redwood");
print("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"));
{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/constructor", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/constructor" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p>Returns a reference to the <a href="en/Core_JavaScript_1.5_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="fullwidth-table">
<tbody><tr>
<td class="header" colspan="2">Property of <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a></td>
</tr>
<tr>
<td>Implemented in:</td>
<td>JavaScript 1.1, NES2.0</td>
</tr>
<tr>
<td>ECMA Version:</td>
<td>ECMA-262</td>
</tr>
</tbody></table>
<h3 name="Description"> Description </h3>
<p>All objects inherit a <code>constructor</code> property from their <code>prototype</code>:
</p>
<pre>o = new Object // or o = {} in JavaScript 1.2
o.constructor == Object
a = new Array // or a = [] in JavaScript 1.2
a.constructor == Array
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>document.constructor == Document
document.form3.constructor == Form
</pre>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Displaying_the_constructor_of_an_object"> Example: Displaying the constructor of an object </h4>
<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>function Tree(name) {
   this.name = name;
}
theTree = new Tree("Redwood");
print("theTree.constructor is " + theTree.constructor);
</pre>
<p>This example displays the following output:
</p>
<pre class="eval">theTree.constructor is function Tree(name) {
    this.name = name;
}
</pre>
<h4 name="Example:_Changing_the_constructor_of_an_object"> Example: Changing the constructor of an object </h4>
<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>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>
{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/constructor", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/constructor" } ) }}
Revert to this revision