Object.prototype.constructor

  • Revision slug: JavaScript/Reference/Global_Objects/Object/constructor
  • Revision title: constructor
  • Revision id: 87964
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 6 words added, 11 words removed

Revision Content

 

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 1.1
ECMAScript Edition ECMAScript 1st Edition

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");
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"));

{{ 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>
<p>Returns a reference to the <a href="/en/JavaScript/Reference/Global_Objects/Object" rel="internal">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/JavaScript/Reference/Global_Objects/Object"><code>Object</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>1.1</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 1st Edition</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");
console.log("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>
<p>{{ languages( { "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/Object/constructor", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/Object/constructor" } ) }}</p>
Revert to this revision