Home > database >  object-oriented
object-oriented

Time:09-30

Programming paradigm
1: imperative programming
Command a machine to do things, so no matter what you want, he will be in accordance with your command implementation,

1: process oriented
Problem solving steps required to, and then step by step implementation,

2: object-oriented
Object-oriented is to decompose a problem affairs into each object, establish the purpose of the object is not in order to complete a step, but in order to describe the behavior of the Chinese step to solve problems,
Note: * * * * is not completely out of object-oriented process oriented, that is to say, even as if the idea of object-oriented, there will also be geared to the needs of process steps, simply by object-oriented can give us save a lot of steps, because these steps by the object itself,

Feature
# # # # # 1: encapsulation
Internal operation hidden in the object, only basic functions exposed to the end user,
# # # # # 2: inheritance
New object inherit the characteristics of an existing object, but will add some new special effects,
# # # # # 2: polymorphic
Different objects can share the same method, but also to be able to use some specific methods to cover the original method,


Class object is a kind of summary, the object is a concrete implementation of a class,

Example:
The class Person {
Public $name;
Public $gender;
Public $age;
The function __construct ($name, $gender, $age) {
$this - & gt; Name=$name;
$this - & gt; Gender=$gender;
$this - & gt; Age=$age;
}
2: declarative programming
Tell the machine what you want, let the machine to figure out how to do,

1: a domain-specific language
DSL: mainly refers to some corresponding specialized in the field of high-level programming languages, and relative to the concept of general purpose programming language,

2: the functional programming
Is a programming model, the computer operation as function of mathematical calculations, in functional programming, the variable is a name, rather than the storage unit,

# # # # a prototype object
In js, each object, the object has a prototype, the prototype object also has a own prototype object, one layer up to find, will eventually reach the null,
# # # #
object classification
1: native objects
Built-in objects
Js itself in the built-in,
Custom objects
The developer custom object

2: the host object
Based on the object to create a new object

Const person={
Very different: 2,
Legs: 2,
Walk () {the console. The log (' walking '); }
}
Const xiejie=Object. The create (person); The console. The log (xiejie. Very different);//2
The console. The log (xiejie. Legs);//2 xiejie. Walk ();//there
The console. The log (xiejie __proto__===the person);//true
Prototype relevant methods
1: the prototype and _proto_ c
Prototype is the constructor of the above an attribute that points to an object, the object is the constructor instantiates the object prototype object instantiation of objects can be found through _proto_ own wanted to object,

Const arr=[1, 2, 3, 4, 5]; The console. The log (Array. The prototype); The console. The log (arr. __proto__);
The console. The log (Array. The prototype===arr. __proto__);
2: Object. GetPrototypeOf () method
Looking for a prototype object

Let arr=[1, 2, 3, 4, 5]. The console log (Object. GetPrototypeOf (arr));//[]
The console. The log (arr. __proto__);//[]
The console. The log (Object. GetPrototypeOf (arr)===arr. __proto__);//true
# # # # # 3: constructor property
Check the object constructor

Const arr=[1, 2, 3, 4, 5]; The console. The log (arr. Constructor);///the Function: Array
4. Instanceof operator
To determine whether an object is an instance of the constructor, return true or false,

Const arr=[1, 2, 3, 4, 5]; The console. The log (arr instanceof Array);//true
The console. The log (arr instanceof Number);//false
IsPrototypeOf () method
Returns a Boolean value, test whether an object is an instance object prototype object,

HasOwnProperty ()
Checks if a property is defined in to think themselves above or from the prototype object inherited, returns a Boolean value,

The constructor

1. The constructor to create objects
Using the function to simulate the other classes in object-oriented languages,
Used for solid column object function, we call this constructor,
Nana:

Const Computer=function (name, price) {
this.name=name;
This. Price=price;
}
Computer. The prototype. ShowSth=function () {
The console. The log (` this is a {this. The name} computer `);
}
When using the new operator when calling a function, the function will return an object, usually, the constructor of this will point to return the object,
Nana:

Const Computer=function (name, price) {
this.name=name; This. Price=price;
}
Computer. The prototype. ShowSth=function () {
The console. The log (this);//print out this is pointing to the object
The console. The log (` this is a {this. The name} computer `);
}
Const=new apple Computer (" apple ", 12000);
The console. The log (apple. Name);//apple
The console. The log (apple. Price);//12000
Apple. ShowSth ();//Computer {name: 'apple', price: 12000}
this is an apple ComputerConst asus=new Computer (" asus ", 5000);
The console. The log (asus. Name);//asus
The console. The log (asus. Price);//5000
The asus. ShowSth ();//Computer {name: 'asus, price: 5000} this is a asustek Computer
2. The constructor explicitly return content
Constructor explicit return type object object difference
Nana:

Const Computer=function (name, price) {
this.name=name;
This. Price=price;
}
Computer. The prototype. ShowSth=function () {
The console. The log (this);//print out this is pointing to the object
}
Const=new apple Computer (" apple ", 12000);
The console. The log (apple. Name);//apple
Apple. ShowSth ();//Computer {name: 'apple', price: 12000}
3. 6 classes in ECMAScript statement
Use the class keyword to declare a class, and from beyond the column object class
Nana:

The class Computer {
//the constructor
The constructor (name, price) {
this.name=name;
This. Price=price; }
//prototype method
ShowSth () {
The console. The log (` this is a ${this. The name} computer `);
}
} const=new apple Computer (" apple ", 12000);
The console. The log (apple. Name);//apple
The console. The log (apple. Price);//12000
Apple. ShowSth ();//this is an apple computer
4. A static method
Also called class methods, that is, by the method of class to invoke the static method is that the benefits of object, should not be instantiated directly through the class method calls could be carried out,
Nana:

The class Computer {
//the constructor
The constructor (name, price) {
this.name=name;
This. Price=price; }
//prototype method
ShowSth () {
The console. The log (` this is a ${this. The name} computer `);
}
//static method
The static comStruct () {
The console. The log (" by computer monitor, the host, keyboard ");
}
}
Computer.com Struct ();//by computer display, host, keyboard
If writing type constructor, also have a way to simulate the static method, the method directly on the constructor,
Nana:

Const Computer=function (name, price) {
this.name=name;
This. Price=price;
}
Computer. The prototype. ShowSth=function () {
The console. The log (` this is a ${this. The name} computer `);
}
//static method directly through Computer the constructor to call
Computer.com Struct=function () {
The console. The log ("???????????????? "); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull