JS programming 03--Object-oriented

Source: Internet
Author: User

There are 2 properties in ECMAScript: Data properties, accessor properties.

A Data property is a feature that specifies some behavior on the specified key of an object, such as whether value can be deleted, modified, or the key can be iterated through. The accessor property does not contain a data value, contains a bunch of Get, set method (not required), read access to the object properties, using getter, setter respectively implemented.

Data properties include:

    • Configurable: Indicates whether the property can be deleted through delete, or the redefined property is modified by default false
    • Enumerable: Indicates for-in loop return property, default False
    • Writable: Indicates whether the property value can be modified by default false
    • Value: Contains values for this data

Once the attribute is defined as not configurable, it cannot be changed back to configurable:

var person = {};object.defineproperty (person,"name", {  configurable:false ,  value:"admin"}); // Direct error: Typeerror:cannot redefine property:name Object.defineproperty (person, "name", {  configurable:true,  value:" Test "})

Because the "name" property is already defined as Configurable:false, the property cannot be re-modified, so the modification will result in an error.

varperson ={name:"I'm a value before the change.", _age:22};object.defineproperty (person,"Age", {get:function(){    return  This. _age; }, set:function(newvalue) {if(NewValue > This. Age) {       This. _age =NewValue;  This. name + = "Haha, I am the modified name value"; }});p Erson.age= 22; Console.log (person);p Erson.age= 23; Console.log (person);

Execution Result:

Note: The attribute underline here is a notation that represents a property that can only be accessed through an object method, so that the read of the property is implemented by using get, set, and only specifying a get representation cannot be written, only specifying that the set indicates that it cannot be read.

So how do you define multiple properties at once, as follows:

var person = {};objetc.defineproperties (person,{  _name:{    value:"Chaozhou"  },  age:{    Value:,    set:function(newvalue) {      this. Age = newvalue;    },    get:function() {      returnthis  . Age;    }  ,  sex:{    value:"male"}  );

JS programming 03--Object-oriented

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.