On JavaScript Object-oriented programming _javascript skills

Source: Internet
Author: User

ECMA-262 defines an object as: "A collection of unordered attributes, whose properties can contain basic values, objects, or functions"

The easiest way to understand an object is by creating an instance of object and then adding properties and methods to it

Copy Code code as follows:

var person = new Object ();
Person.name = "Xulei";
Person.age = "23";
Person.job = "Front-End Engineer";
Person.sayname = function () {
alert (this.name);
}

You can also write this

Copy Code code as follows:

var person = {
Name: "Xulei",
Age:23,
Job: "Front-End Project",
Sayname:function () {
Alert (this.name)
}
}

Attribute type: Data properties and access to their properties

1, data properties, there are 4 characteristics that describe their behavior
[Configurable]: Indicates whether the attribute can be deleted by deleting the property, whether the attribute is modified, or whether the property can be modified to an accessor property, and the default is True
[Enumerable]: Indicates whether the property can be returned by for-in, and the default is True
[Writable]: Indicates whether the property can be modified, the default value is True
[value]: The data value that contains this property. The default value is undefined

Copy Code code as follows:

var person = {
Name: "Xulei"
}

A person object is created here and the value is "Xulei"

To modify the default attribute of a property, you must use the ECMAScript5 Object.defineproperty (the object that contains the property, the name of the property, the Descriptor object)
Descriptor objects must be configurable, enumerable, writable, value

Copy Code code as follows:

var peron = {}
Object.defineproperty (Peron, "name", {
writable:false,//property cannot be modified
Value: "Xu Lei-xulei"
});

alert (peron.name);//Xu Lei-xulei
Peron.name = "Xu Lei";
alert (peron.name);//Xu Lei-xulei

The above operation is ignored in the non strict mode, if the exception is thrown in strict mode
Once the attribute is defined as not configurable, it cannot be changed back to configurable.
In most cases, it is not necessary to take advantage of these advanced features provided by the Object.defineproperty () method. But it's very useful for understanding JavaScript.
It is recommended that readers do not use this method on IE8.

2, access to its properties, there are 4 characteristics
[Configurable]: Indicates whether the attribute can be deleted by deleting the property, whether the attribute is modified, or whether the property can be modified to an accessor property, and the default is True
[Enumerable]: Indicates whether the property can be returned by for-in, and the default is True
[Get]: a function that is called at read time
[Set]: a function that is called when the property is written

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.