JavaScript Object Constructor Property

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.