Overview
Returns a pointer to a function reference that created the object's prototype. Note that the value of this property is the function itself, not a string that contains the name of the function. For raw values (such as 1, true
or " test
"), the property is read-only.
Describe
All objects inherit a property from its prototype constructor
:
var New // or o = {}o.constructor = = Objectvarnew Array // or a = [] A.constructor = = Arrayvarnew number (3 = = number
Even though some DOM objects are not generated by constructors, you can still compare them with the corresponding constructors. Like what:
Document.constructor == = = Form;
Example 1: Print out a constructor for an object
The following example first creates an object that constructs a prototype (that is, a constructor) Tree
and a prototype theTree
. The properties of the object are then printed out theTree
constructor
.
function Tree (name) { this. Name = name;} var New Tree ("Redwood""Thetree.constructor is" + thetree.constructor);
Print output:
function Tree (name) { this. Name = name;}
Example 2: Changing the value of the constructor property of this object
The following example shows how to modify the value of the constructor property of a primitive type object. Only true, 1 and "test" are not affected, because they are created as read-only native constructors (native constructors). This example also shows that it is not safe to rely on an object's constructor property.
functionType () {};varTypes = [ NewArray, [],NewBoolean,true,//remains unchanged NewDate,NewError,NewFunction,function() {}, Math,NewNumber ,1,//remains unchanged NewObject, {},NewRegExp,/(?:) /, NewString,"Test"//remains unchanged]; for(vari = 0; i < types.length; i++) {Types[i].constructor=Type; Types[i]= [Types[i].constructor, types[i]instanceofType, types[i].tostring ()];}; Console.log (Types.join ("\ n"));
Specification
Spec Version |
Canonical State |
Annotations |
ECMAScript 1st Edition. Implemented in JavaScript 1.1 |
Standard |
Initial definition. |
ECMAScript 5.1 (ECMA-262) Objectprototype.constructor |
Standard |
|
ECMAScript 6 (ECMA-262) Object.prototype.constructor |
Draft |
|
Browser compatibility
Feature |
Chrome |
Firefox (Gecko) |
Internet Explorer |
Opera |
Safari |
Basic Support |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
(Yes) |
JavaScript Object Constructor Property