Cultivation of Web Front-end development-Object-Oriented Programming

Source: Internet
Author: User

Okay, it's been a long time before I write something!

Two days ago, I asked a technician what the object orientation is. He joked that he was facing my object (boyfriend )... Sorry! Haha, today we see Object-Oriented Programming for "Writing high-quality code. Recorded!

First, give an example of a phone book:

// Define the phone book 1

VaR phonebook = [

{Name: "adang", tel: "1111111 "},

{Name: "ht", tel: "222222 "},

......

];

// Define the phone book 2

VaR phonebook2 = [

{Name: "ad", tel: "111155 "},

{Name: "he", tel: "2222222255 "},

......

];

// Query the phone number

Function gettel (ophonebook, oname ){

VaR Tel = "";

For (VAR I = 0; I <ophonebook. length; I ++ ){

If (ophonebook [I]. Name = oname ){

Tel = ophonebook [I]. Tel;

Break;

}

}

}

// Add record

......

// Delete a record

......

// Query the "niaoren" phone number from the phone book

VaR STR = gettel (phonebook, "niaoren ");

Alert (STR );

// Add the niaoren phone number to the phone book

......

// Delete the niaoren phone number in the phone book

......

// Query the niaoren record in phone book 2

......

This programming method divides the program into two parts: "data" and "processing function". The program uses "processing function" as the core. If you want to execute any function, pass "data" to the corresponding "processing function" and return the results we need. This programming method is process-oriented.

The process-oriented approach is easy to use, but it has three problems:

1. there is no direct association between data and processing functions. When performing an operation, we should not only select the corresponding processing function, but also prepare the data required for processing the function, in other words, we need to focus on processing functions and data while executing operations.

2. both data and processing functions are exposed in the same scope. There is no private or public concept. All data and processing functions in the entire program can access each other. In the development stage, the development speed may be very fast, however, in the post-development and maintenance stages, due to the tight coupling of the entire program, any processing function and data may be associated with other places, making it easy to take a long time and making modifications more difficult.

3. process-oriented thinking is a typical computer thinking mode-input data flying processor, computation performed inside the processor, and result returned by the processor. In real life, our thinking is not like this-all things in real life are stateful and Action-driven. It is difficult to describe the book with object-oriented process thinking. Because the process-oriented approach is to describe "actions", start points (initial data), and action processes (initial data is transmitted to the processing function for execution ), there is an end point of an action (the processing function returns the processing result), while the objective world exists as "objects". The object is stateful and has an action, and the object itself is an objective existence, there is no start point and no end point. Only actions of objects can be described with process-oriented thinking.

Next, let's talk aboutObject-Oriented Programming,

What Is Object-Oriented Programming? Object-Oriented programming is a programming method that skips computer thinking and enables thinking in life to be programmed. Process-oriented thinking is to describe "actions" one by one, while the object-oriented thinking is mapped to the program-if the programming language you use supports object-oriented, in a program, an object is called an object. An object consists of two parts: "attribute" and "behavior ", corresponds to the "State" and "action" of objects in the objective world ". It is described in process-oriented mode.

Surface phase process Programming

VaR name = "adang ";

VaR state = "Awake ";

VaR say = function (oname ){

Alert ("I'm" + oname );

}

VaR sleep = function (ostate ){

Ostate = "asleep"

}

Say (name );

Sleep (State );

The adang object is called first, and then the function is called to describe the adang object.

Attribute is actually a variable, that is, process-oriented data, and behavior is actually a function, that is, a process-oriented function. What's not needed is that object-oriented data and processing functions are defined inside the object and exist as attributes and actions of the object outside the object, attributes and behaviors can be called by Object Attributes and object behaviors, so that the program can be described in a real-world way of thinking. Within an object, the attributes and behaviors of an object are associated by the this keyword (or other keywords). For example, you can use this when calling the name attribute. name to call the attributes of your own object.

All data and processing functions of process-oriented programming are public. The whole programming thinking is to define data, define processing functions, and then pass the data to the processing functions for processing, processing functions can also be called to each other. Data and processing functions are closely coupled. The thinking process of face object programming is to define objects one by one. Objects have their own attributes and behaviors, because attributes and behaviors are subordinate to objects, with the concept of "object inside" and "Object outside", the whole program can be composed of a bunch of objects, and objects may communicate with each other. To achieve this communication, objects design part of their attributes and behaviors as public, exposing them as an excuse for communication. Communication between objects is based on interfaces. Of course, we can set all the attributes and actions of an object to be public and all of them serve as interfaces. However, the more interfaces, the closer the coupling between objects, and the more difficult maintenance. Therefore, we usually try to set the attributes and methods of the object to private, as long as necessary attributes are set to public. However, the fewer public attributes and public behaviors of objects, the worse the scalability of the entire program. Therefore, we need to weigh between public and private attributes. Set attributes and actions to private without affecting scalability. (To be continued)

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.