Object.prototype.constructor

  • Revision slug: JavaScript/Reference/Global_Objects/Object/constructor
  • Revision title: constructor
  • Revision id: 87957
  • Created:
  • Creator: Andrea@3site.it
  • Is current revision? No
  • Comment /* Examples */

Revision Content

Summary

Specifies the function that creates an object'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")
document.writeln("<B>theTree.constructor is</B> " +
   theTree.constructor + "<P>")

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" variables 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"));
{{ wiki.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>Specifies the function that creates an object'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")
document.writeln("&lt;B&gt;theTree.constructor is&lt;/B&gt; " +
   theTree.constructor + "&lt;P&gt;")
</pre>
<p>This example displays the following output:
</p>
<pre>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" variables 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>
{{ wiki.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