Three major Object-oriented features
Package
The so-called encapsulation means encapsulating objective things into abstract classes, and the class can only allow trusted classes or objects to operate on information that is untrusted. Encapsulation is one of the object-oriented features and is the main feature of object and class concepts. Simply put, a class is a logical entity that encapsulates data and code that operates this data. Inside an object, some code or some data can be private and cannot be accessed by the outside world. In this way, objects provide different levels of protection for internal data to prevent unrelated parts of the program from accidentally changing or incorrectly using private parts of the object.
We use mixing in the vue project to bring up public code and mix it into every required component to make our code more concise
We can also encapsulate some common methods and tools to reuse codes to make our code simpler
inherit
Inheritance refers to a method that allows an object of a certain type to obtain the properties of another type of object. It supports the concept of classification by level. Inheritance refers to the ability to use all the functions of an existing class and extend these functions without rewriting the original class. A new class created through inheritance is called a "subclass" or a "derived class", and an inherited class is called a "base class", a "parent class" or a "superclass". The process of inheritance is from general to special. To achieve inheritance, it can be achieved through "Inheritance" and "Composition". There are two ways to implement the concept of inheritance: implementation of inheritance and interface inheritance. Implementing inheritance refers to the ability to directly use the properties and methods of the base class without additional encoding; interface inheritance refers to the ability to use only the names of properties and methods, but subclasses must provide implementation;
Several ways to implement inheritance
Call inheritance
function a (){ =111 } function b(){ (this) } let d = new b() () // 111
The above code is equivalent to function b inheriting the private properties of function a, and inheriting it by changing this of the parent class
Prototype inheritance
function a (){ =111 } =function(){ return 'Hello' } function b(){ // (this) } =new a() =b let d = new b() () // 111 (()) // Hello
Prototype inheritance: By assigning instances of the parent class to the prototype of the child class, this method subclass can inherit the private methods of the parent class or the private methods of the parent class.
Parasitic combination inheritance
function a (){ =111 } =function(){ return 'Hello' } function b(){ (this) } =() let d = new b() () // 111 (()) // Hello
Parasitic combination inheritance is to use call inheritance to change this, realize private inheritance and private ownership, and use public inheritance to implement public ownership.
es6 extends inheritance
class parent{ constructor(){ =1 } name(){ return 2 } } class child extends parent{ } let A = new child() () // 1 (()) // 2
Here, the subclass inherits the private and public ownership of the parent class through the keyword extends. Here you need to note that if the constructor is written in the subclass, you must write super, otherwise an error will be reported.
class parent{ constructor(){ =1 } name(){ return 2 } } class child extends parent{ constructor(){ // If you don’t write super here, you will report an error. The error message is as follows } } // ncaught ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor at new child
Polymorphic
The so-called polymorphism refers to the same method of a class instance having different manifestations in different situations. The polymorphic mechanism allows objects with different internal structures to share the same external interface. This means that although specific operations for different objects are different, they (those operations) can be called in the same way through a common class.
Reload
Function overloading refers to a group of functions with the same function name and different parameter lists within the same scope. This group of functions is called overloaded functions. Overloaded functions are usually used to name a group of functions with similar functions. This reduces the number of function names and avoids the pollution of namespace, which is of great benefit to the readability of the program.
- js finds the corresponding function object through the function name
- Then match the parameters of the function as defined and the expression parameter list in order. The extra parameters are discarded, and the insufficient parameters are processed as undefined.
- Then execute the function code.
// Reload according to the number of arguments function add() { var sum = 0 ; for ( var i = 0 ; i < ; i ++ ) { sum += arguments[i]; } return sum; } (add()) // 0 (add(1,2)) // 3 (add(1,2,3)) // 6
Rewrite
"The pointer in the instance only points to the prototype, not to the constructor".
"Rewriting prototype objects cuts off the relationship between an existing prototype and any previously existing object instance; they still refer to the original prototype."
var parent = function(name,age){ = name; = age; } = function(){ (+":"+); } var child = function(name,age){ (this,name,age); } // inheritance = (); // = new parent(); = child; // rewrite function = function(){ ('I am '++":"+); } var obj = new child('wozien','22'); ();
The above code uses parasitic combination inheritance to realize that the subclass private inheritance of the parent class private ownership, and the subclass public inherits the parent class public ownership, so as to override the showProper of the parent class.
5 major principles for object-oriented
- Single responsibility principle
- Open and closed principle
- Substitution principle
- Dependence principle
- Interface separation principle
Single responsibility principle
The single responsibility principle is what we say is a method that only does one thing, for example, the current project structure follows the single responsibility principle.
Open and closed principle
The principle of openness and closure is to close modifications and openness to extensions
class a { add(){ return 11 } } class b extends a{ } let c = new b() (()) // 111
We can use extends to inherit the parent class, and we can modify the add function in b to achieve closed modification and open extension
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.