JavaScript Object-Oriented notes understanding objects

Source: Internet
Author: User

0x00 background
We know that an object-oriented language has a flag that is the concept of a class that allows you to create multiple objects with the same properties and methods. But there is no concept of a class in ECMAScript, so its object is a little different from the objects in other classes based languages, and it is based on the Object-oriented object of the prototype. ECMA-262 defines an object as "a collection of unordered attributes, whose properties can contain basic values, objects, or functions." Each object is created from a reference type, either as a native reference type or as a custom type.
Let's start by creating an object in the simplest way.

The code is as follows Copy Code

var person = new Object ();
Person.name = "Gothic";
Person.age = 21;
Person.job = "f2e";
Person.sayname = function () {
alert (this.name);
};

In this object we added three attributes (Name,age,job) and one method (Sayname ()). Of course, we can also rewrite this object with the literal volume of objects:

  code is as follows copy code

var person = {
Name: Gothic,
age:21,
Job: f2e,
Sayname:function () {
alert (this.name);
}
}

 

The

0x01
ECMAScript has two properties: Data properties and Accessor Properties
1. Data Properties
The Data property contains the position of a data value where the value can be read and written. It has four attributes:
Configurable: Indicates whether the property can be deleted by deleting the property, whether the attribute is modified, and the default value is true.
Enumerable: Indicates whether the property can be returned through a for-in loop. As in the first example above, the property is defined directly on the object, and the default value is true.
Writable: Indicates whether the value of the property can be modified. The default value is true.
Value: Contains the data value of this property, when reading the property, reading from this location, writing the property, save the new value in this location. The default value is: undefined.
If you want to modify property default attributes, you must use the Object.defineproperty () method. This method takes three parameters: the object in which the property is located, the name of the property, and a descriptor object. The attribute of the Descriptor object must be configurable,enumerable,writable,value. By setting one or more of these values, you can modify the corresponding attribute values, as follows:

The code is as follows Copy Code

var person={};
Object.defineproperty (person, "name", {
Writable:false,
Value: "Gothic"
}); Www.111cn.net
alert (person.name);//"Gothic"
Person.name= "Dyy";
alert (person.name);//"Gothic"

Here we can also change the writable:false to Configurable:false, which means that the attribute cannot be deleted from the object, that is, if this is called Delete on this property, nothing will happen in the strict mode, but an error will occur in strict mode.
And this is an error if you want to use the Object.defineproperty () method to modify an attribute other than the writable above, because Object.defineproperty () can be invoked to modify the same property. However, after setting the configruable to False, there is a limit.

In general, when Object.defineproperty () is invoked, the default values for several attributes are false if not specified.

0x02 Accessor Properties

The

Accessor property has the following four attributes:
Configurable: Indicates whether the property can be deleted by deleting the attribute, and whether the attribute can be modified, and the default value is true.
Enumerable: Indicates whether the property can be returned through a for-in loop. The default value is true.
Get: The function that is called when the property is read. Default value: Undefined.
Set: The function that is called when the property is written. The default value is: undefined.
Remember that the properties of accessors cannot be defined directly, and must be defined using Object.defineproperty (). is as follows:

  code is as follows copy code

var book ={
_year:2004,
Edition:1,
};
Object.defineproperty (book, ' Year ', {
Get:function () {
return this._year;
},
Set:function (newvalue) {
if (newvalue >2004) {
This._year = newvalue;
This.edition + = newvalue–2004;
}
}
});
Book.year = 2005;
Alert (book.edition);//2

The above code accessor property year contains a getter function and a setter function, where the underscore in front of this _year is a common notation used to represent attributes that can only be accessed through an object method, and the Getter function returns the value of _year. The setter function determines the correct version by calculation. Of course, sometimes we don't have to specify both getter and setter, only to specify that the getter indicates that the property cannot be written, and that the given setter indicates that the property cannot be read, otherwise it will return undefined in the strict mode, and there will be an error in the tight pattern.
2 defines multiple properties
uses the Object.defineproperties () method to define multiple properties at once. This method accepts two object arguments: An object is an object whose properties you want to add and modify, and a second object whose properties correspond to the property one by one that you want to add or modify in the first object. is as follows:

The code is as follows Copy Code

var book = {};
Object,defineproperties (book,{_year:{
value:2004
},
Edition:{value:1},
year:{
Get:function () {
return this._year;
}, Www.111cn.Net
Set:function (NewValue) {
if (newvalue>2004) {
This._year = newvalue;
this.edition+=newvalue-2004;
}
}
}
});

3 Attributes of Read properties
Use the Object.getownpropertydescriptor () method to obtain a descriptor for the given property. This method takes two parameters: the object in which the property is located and the name of the property whose descriptor is to be read. If you get accessor properties, the value of this object is: Configurable,enumerable,get and set, and if it is a data property, the object's properties are: Configurable,enumerable,writable,value.

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.