Notes-[Object-oriented]-JS-based programming-[1]

Source: Internet
Author: User

Object-oriented (OOP): is a development process, with an object-oriented programming thinking to develop.

In JS, we generally use process-oriented development.

Object-oriented Features: abstraction, encapsulation, inheritance, polymorphism

First look at how custom objects are written: Customize a person's object, the person's name, age, or talk. In the following, the name is the attribute of this person, speaking is the person's behavior, can also be called methods. This can also be understood as a simple object-oriented programming. Where the object comes from, such as Var arr=new array (); This is an array of objects, from the JS of a default system method class.

For example: Define a person's class, each person is one of the objects of this person's class, and people have common properties and methods, such as name, can talk and so on. This public property and public method are also called

Exposes properties and exposes methods.

<script type= "Text/javascript" >
var people={
Name: ' WT ',
Age:22,
Calls:function () {
Alert (' OK ');
}
};
alert (people.name);
People.calls ();//ok
</script>

To customize a class in JS is to use function to do keywords; to customize a human. Set its public properties (Name,age,) and public methods (talk)

such as function person (name,age) {//This also called a constructor is also a class

This.name=name;//this is an implicit pointer to an instantiated object where name is a property

This.age=age;

This.talk=function () {//This is a public method

alert (this.name);

}

}

Instantiate an object called Xiaoming

var xiaoming=new person (' xiaoming ', ' 25 ');

Call Xiaoming's age and name and talk about this behavior in the following way

alert (xiaoming.name);//xiaoming

alert (xiaoming.age);//25

Xiaoming.talk ();//xiaoming

Above is a simple example of object-oriented programming, and how to customize a class and set the invocation of properties and methods and properties and methods.

/*****************/

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.