JavaScript Object-oriented notes (i)

Source: Internet
Author: User

ECMAScript Object (hereinafter referred to as the object):

ECMA-262 defines an object as a collection of unordered properties whose properties can contain basic values, objects, or functions.

Each property or method of an object has a name, and each name is mapped to a value. Values can be data or functions.

Each object is created from a reference type that can be a native type or a developer-defined type.

There are two ways to create a custom object:

1. Create an object instance and add properties and methods to it, as shown below.

var New  = ' Xingba '= ' + '= ' front-end Engineer 'function() {  alert (  This . Name);

2, the object literal, as shown below.

var person = {  name:' Xingba ', age  :' $ ',  job:' front-end Engineer ' ,  sayname:function() {    alert (this. Name);  }}

The above objects are created with properties and methods. While these attributes are created with some eigenvalues, JS defines their behavior through these eigenvalue values.

Property Type:

There are two types of properties in ECMAScript: Data properties and accessor properties.

1. Data properties

The data attribute has 4 attributes that describe its behavior.

Configurable: Indicates whether the property can be redefined by deleting the property through the delete, whether the attribute is modified, or whether the property can be modified as an accessor property. The default is true.

Enumerable: Indicates whether the property can be returned through the for-in loop. The default is true.

Writable: Indicates whether the value of the property can be modified. The default is true.

Value: The data value that contains this property. When reading a property value, read from this position, and when writing the value of the property, save the new value in this position.  The default is undefine. For example:

var person = {   = ' Xingba ';};

At this point the value attribute will be set to ' Xingba ', and any modification to this value the soy Milk cup is reflected in this position.

To modify the property default attribute, you must use the ECMAScript5 object.defineproperty () method. The method receives three parameters: the object where the property resides, the property name, and a descriptor . The properties of the Descriptor object must be: Configurable, enumrable, writable, and value. set one or more of these values to modify the corresponding attribute values . For example:

var person = {};object.defineproperty (person,name,{    writable:false//  The default is true, which is set to false to indicate that the Name property is read-only and cannot be modified  value    : ' Xingba '  // Sets the value of name to ' Xingba '  }); Console.log (person.name);   // ' Xingba 'person.name  = ' bao ';  // re-set the name value console.log (person.name);  // ' Xingba '

And in strict mode, the above statement will error, as shown in.

The same configurable is set to false to indicate that the property cannot be removed from the object. If you call delete on this property, you will get an error in strict mode and nothing will happen in strict mode.

And there's a reference to setting the configurable value to set whether attributes can be modified. Once the configurable is set to False, the attribute is defined as not configurable and cannot be changed back to configurable. At this point, the call to the Object.defineproperty () method to set the attribute value will error, and if not specified when calling the Object.defineproperty () method, Configurable,enumerable, The default value of writable is false, as shown below.

2. Accessor Properties

Accessor properties do not contain data values and contain a pair of getter and setter functions (two functions are not required). These two characteristics can be used to differentiate between accessor properties and data properties. Accessor properties cannot be defined directly and must be defined using Object.defineproperty ():

When the accessor property is read, the Getter function is called, which is responsible for returning a valid value, and when the accessor property is written, the setter function is called and the new value is passed in, which is responsible for deciding what to do with the data, as shown below.

Use the Object.defineproperties () method to define multiple properties for an object

var person = {};object.defineproperties (person,{  _name:{    value:' Xingba '  },  age:{    Value:  name:{    get:function() {        Returnthis. _name    },    set:function(newName) {       this. _name = newName;}}  );

JavaScript Object-oriented notes (i)

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.