Js object-oriented programming notes

Source: Internet
Author: User

Some Notes on js object-oriented programming. In js, object-oriented programming is a bit like java or c ++. Below I will give you some notes on js object-oriented programming, I hope it will help you.

Many background languages, such as c ++, java, and php, are object-oriented programming languages. In JavaScript, object-oriented programming is not perfect. Although JavaScript contains many objects, however, object orientation cannot be equal to objects. So today, I will explain the object orientation in js to you in detail.

1. What is object-oriented

In short, object-oriented has three main features: encapsulation, inheritance, and polymorphism.

1. encapsulation: ability to store relevant information (whether data or methods) in objects
2. Inheritance: the ability of attributes and Methods derived from another class (or multiple classes)
3. Polymorphism: the ability to write functions or methods that can run in multiple ways

Ii. Advantages of object-oriented

1. Easy to maintain
The structure is designed with object-oriented thinking, which is highly readable. Because of the existence of inheritance, maintenance is only implemented in local modules even if the requirements are changed. Therefore, maintenance is very convenient and cost-effective.
2. High quality
Existing classes that have been tested in the field of previous projects can be reused during design so that the system meets business needs and has a high quality.
3. High Efficiency
During software development, the real-world things are abstracted according to the design needs to generate classes. Using this method to solve problems is close to the way of daily life and natural thinking, which is bound to improve the efficiency and quality of software development.
4. Easy scalability
Because of the inheritance, encapsulation, and polymorphism characteristics, the system structure with high cohesion and low coupling is designed, making the system more flexible and easy to expand, and the cost is low.

Iii. Object-oriented writing in js

1. Factory method

The Code is as follows: Copy code

Function createPerson (name, age)
{
Var person = new object ();
Person. name = name;
Person. age = age;
Person. showPerson = function ()
{
Alert ("My name" + this. name + ", I am" + this. age + "years old ");
}
}
Var p1 = createPerson ("huanghao", 23 );
P1.showPerson (); // my name is huanghao. I am 23 years old.

2. constructor Method

The Code is as follows: Copy code

Function createPerson (name, age)
{
This. name = name;
This. age = age;
This. showPerson = function ()
{
Alert ("My name" + this. name + ", I am" + this. age + "years old ");
}
}
Var p1 = new createPerson ("huanghao", 23 );
P1.showPerson ();

Note: The first two methods have drawbacks, that is, every time a new function is created, the function will be re-built. Suppose I create another var p2 = new createPerson ("xiaotian", 20 ); next, let's play alert (p1.showPerson = p2.showPerson). We are surprised to find that the result is false, which means that the showPerson method of the new object is not the same method, so the prototype came into being.

3. Hybrid prototype/construction and prototype

The Code is as follows: Copy code

Function createPerson (name, age)
{
This. name = name;
This. age = age;
}
CreatePerson. prototype. showPerson = function ()
{
Alert ("My name" + this. name + ", I am" + this. age + "years old ");
}
Var p1 = new createPerson ("huanghao", 23 );
P1.showPerson ();

Create another var p2 = new createPerson ("blue", 33); alert (p1.showPerson = p2.showPerson) // true

The above method is a combination of prototype and constructor, which is often used when js is object-oriented. This method is used to write attributes in constructor, the method is written in prototype.

4. this point in js object-oriented

Js object orientation is usually confusing when this points to a problem. In the small Editor's experience, there are usually two places where this points to a problem and an error occurs. The first is when a timer is used, the second is to add events to objects, so pay special attention to them.

Summary: The above is the object-oriented method of js loyalty. The third method can be used to simulate the object-oriented method of mainstream programming languages. In addition, many questions have been asked during interviews,

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.