Constructor JavaScript constructor mode.

Source: Internet
Author: User

Constructor mode:

In constructor mode, by adding the new keyword in front of the constructor, it tells JavaScript to instantiate an object as if it were using a constructor, and the object member is defined by the function.

constructor, use this to refer to the newly created object.

There is a property named prototype in JavaScript, and when the JavaScript constructor is called to create an object, the new object will have all the properties of the constructor prototype.

To set properties:

1) use. 2) Use []

3) Use object.defineproperty

4) Use object.defineproperties

varNewObject = {};//var newObject = new Object ();Newobject.somekey= ' Hello world! ';varKey =newobject.somekey;newobject[' Somekey ' = ' Hello world!!! '; Object.defineproperty (NewObject,"Somekey", {value:' For more control of the property\ ' behavior ', writable:true, Enumerable:true, Configurable:true});varDefineprop =function(obj, key, value) {Config.value=value; Object.defineproperty (obj, key, config);}varperson = object.create (NULL);d Efineprop (person,' Car ', ' DeLorean ');d Efineprop (person,' dateOfBirth ', ' 1981 ');d Efineprop (person,' Hasbeard ',false); Object.defineproperties (NewObject, {"Somekey" : {        "Value": "Hello World", writable:true    },    "Anotherkey": {value:"Foo Bar", writable:false    }});

Constructor JavaScript constructor mode.

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.