Introduction to javascript Object-Oriented Programming and Object-Oriented Programming

Source: Internet
Author: User

Introduction to javascript Object-Oriented Programming and Object-Oriented Programming

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

The simplest way to understand an Object is to create an Object instance and add attributes and methods to it.

Copy codeThe Code is 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

Copy codeThe Code is as follows:
Var person = {
Name: "xulei ",
Age: 23,
Job: "front-end project ",
SayName: function (){
Alert (this. name)
}
}

I. attribute type: data attributes and access attributes

1. Data attributes: four characteristics that describe their behaviors
[Retriable]: indicates whether the attribute can be deleted through delete to redefine the attribute, whether the attribute can be modified, or whether the attribute can be modified to the accessors attribute. The default value is true.
[Enumerable]: Indicates whether an attribute can be returned through for-in. The default value is true.
[Writable]: indicates whether the attribute can be modified. The default value is true.
[Value]: the data Value that contains this attribute. The default value is undefined.

Copy codeThe Code is as follows:
Var person = {
Name: "xulei"
}

A person object is created here, and the value is "xulei"

To modify the default properties of an attribute, you must use the Object. defineProperty of ECMAScript5 (the Object where the attribute is located, the name of the attribute, and the descriptor Object)
The descriptor object must be retriable, enumerable, writable, and value.

Copy codeThe Code is as follows:
Var peron = {}
Object. defineProperty (peron, "name ",{
Writable: false, // attributes 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 operations are ignored in non-strict mode, and an exception is thrown in strict mode.
Once the attribute is defined as unconfigurable, it cannot be changed back to configurable.
In most cases, it is unnecessary to use these advanced functions provided by the Object. defineProperty () method. But it is very useful for understanding javascript.
We recommend that you do not use this method on ie8.

2. Access its attributes with four features
[Retriable]: indicates whether the attribute can be deleted through delete to redefine the attribute, whether the attribute can be modified, or whether the attribute can be modified to the accessors attribute. The default value is true.
[Enumerable]: Indicates whether an attribute can be returned through for-in. The default value is true.
[Get]: The function called during reading
[Set]: The function called when writing an attribute

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.