What an object really is, we can understand it from two levels.

(1) An object is an abstraction of a single thing.

A book, a car, a person can be an object, as can a database, a web page, a connection to a remote server. When objects are abstracted into objects, the relationship between objects becomes the relationship between objects, so that you can simulate real situations and program objects against them.

(2) An object is a container that encapsulates properties and methods.

Properties are the state of an object, and methods are the behavior of an object (to accomplish some task). For example, we can abstract an animal as an animal, use “genus” to record which animal it is, and use “method” to indicate certain behaviors (running, hunting, resting, etc.).

In practical development, an object is an abstract concept that can be understood simply as a set of data or a set of features.

Ecmascript-262 defines an object as an unordered collection of properties that can contain basic values, objects, or functions. Strictly speaking, this is equivalent to saying that an object is a set of values in no particular order. Each property or method of an object has a name, and each name maps to a value.

Tip: Each object is created based on a reference type, which can be a native type built into the system or a developer-defined type.

Object Oriented Programming, or OOP, is a kind of Programming development idea. It abstracts all kinds of complex relationships in the real world into one object, and then completes the simulation of the real world by the division of labor and cooperation between objects. In the idea of object-oriented program development, every object is a functional center, with a clear division of labor, can complete the task of receiving information, processing data, sending information and so on. Therefore, object-oriented programming is flexible, reusable, highly modular, and easy to maintain and develop. Compared with traditional procedural programming consisting of a series of functions or instructions, it is more suitable for large-scale software projects with cooperation between multiple people.

Object-oriented and Process-oriented:

  • Process-oriented is hands-on, detailed, thorough, closely followed, and methodical

  • Object orientation is finding an object and directing the result

  • Object orientation turns the executor into the conductor

  • Object orientation is not a substitute for process orientation, but an encapsulation of process orientation

Object-oriented features:

  • encapsulation

  • inheritance

  • abstract

The basic embodiment of object – oriented program

In JavaScript, all data types can be treated as objects, or you can customize objects. A custom object data type is the object-oriented concept of a Class.

Let’s use an example to illustrate the difference between procedural and object oriented program flow.

Suppose we are dealing with a student’s score sheet. To represent a student’s score, a process-oriented program can be represented as an object:

Var std1 = {name: 'Zhang SAN ', score: 98} var std2 = {name:' Li Si ', score: 81} var std3 = {name: 'Wang Wu ', score: 98}Copy the code

Student scores can be processed by functions such as printing students’ grades:

Function printScore (student) {console.log(' name: '+' score: '+' score: '+' score: '+' score: ')}Copy the code

If we adopt the idea of object-oriented programming, the first thing we should think about is not the execution process of the program, but the data type of Student should be

As an object, this object has two properties, name and Score. If you want to print a student’s grade, you must first

You have to create an object that corresponds to the student, and then send a printScore message to the object, and let the object print out its own data.

Abstract Data behavior template (Class) :

function Student(name, score) { this.name = name; this.score = score; This.printscore = function() {console.log(' name: '+ this.name +' + 'score:' + this.score); }}Copy the code

Create an Instance object based on the template:

var std1 = new Student('Michael', 98)

var std2 = new Student('Bob', 81)
Copy the code

Instance objects have their own specific behavior (sending messages to objects) :

PrintScore () // => Name: Michael score: 98 std2.printScore() // => Name: Bob score: 81Copy the code

Object-oriented design ideas are derived from nature, where the concepts of classes and instances are natural.

Class is an abstract concept. For example, when we define Class — Student, we refer to the concept of a Student, and Instance is a

One specific Student, for example, Michael and Bob are two specific students.

So, the idea of object-oriented design is:

  • Create an Instance based on Class(constructor)

  • The result of directing Instance

  • Object-oriented is more abstract than a function, because a Class contains both data and methods to manipulate that data.