One: JS object-oriented programming
Es5 writing
Function Person(name) {this._name = name; this.getName = function () { console.log(this._name); }; this.setName = function (name) { this._name = name; }; } var p = new Person(" Person "); p.getName(); // Zhang SAN p.setname (" Li Si "); p.getName(); / / li siCopy the code
The latest version of ES6
Constructor (name) {this._name = name; // Class Person {constructor(name) {this._name = name; } getName() { console.log(this._name); } setName(name) {this._name = name}} let p = new Preson(' three ') p.get_name (); // setname (' li Si '); p.getName(); / / li siCopy the code
Two: The three characteristics of the object
The three characteristics are encapsulation, inheritance and polymorphism
1: encapsulation
The methods and classes we usually use are all encapsulated. When we encounter a piece of functional code repeatedly used in many places in project development, we can separate it into a functional method, so that we can call it directly where we need to use it.
2: inheritance
Inheritance is mainly used in our project development to inherit a parent class for a subclass. Here is how ES6 inheritance is written
Class Father {constructor(name) {this._name = name; }// Instance method, using the instance object to call getName() {console.log(this._name); }} static hitSon() {console.log(" console.log ")}} class Son extends Father {constructor(name, constructor) Age) {// pass the data of the subclass to the parent class when instantiating the subclass. this._age = age; } } Father.hitSon(); Var DaMing = new Father(' Father '); DaMing.getName(); / / dad DaMing. HitSon (); // Uncaught TypeError: DaMing. HitSon is not a function var XiaoMing = new Son(' XiaoMing ', 15); // Uncaught TypeError: DaMing. XiaoMing.getName(); / / XiaoMing XiaoMing. HitSon (); // Uncaught TypeError: XiaoMing.hitSon is not a functionCopy the code
Note: Inheritance inherits instance properties and methods of the parent class, not static properties and methods, and static methods can only be called by class name.
3: polymorphism
The specific manifestations of polymorphism are method overloading and method rewriting:
Method overloading: Overloading is when different functions use the same function name but have different numbers or types of arguments. Call different functions based on their arguments
Method rewriting: Rewriting (also called overwriting) is the reimplementation of a virtual function in a base class in a derived class. That is, the function name and parameters are the same, but the implementation body of the function is different
Add a work () method to work ();
Class Father {constructor(name) {this._name = name; } // Instance method, using the instance object to call getName() {console.log(this._name); } work() {console.log(' MY job is dead work, Static hitXiaoMing() {console.log(" console.log ")}} class Son extends Father {// Static hitXiaoMing() {console.log(" console.log ")}} class Son extends Father { Constructor (name, age) {constructor(name, age) {constructor(name, age) {constructor(name, age) {constructor(name, age); this._age = age; } work() {console.log(' My job is to study hard and make progress every day. ')}} var Father = new Father(' Father '); Work () // I work my ass off to bring home the bacon. Var XiaoMing = new Son(' XiaoMing ', 15); XiaoMing.work(); // My job is to study hard and make progress every day.Copy the code
Above we overrode the work () method of our parent class.
Advantages of three features:
Encapsulation: The advantage of encapsulation is that the definition can only operate on the properties of the class, the external can not dictate these properties, want to change, you can only define the encapsulation method;
Inheritance: Inheritance reduces code redundancy and omits a lot of duplicate code. Developers can define attributes and methods that all subclasses must have from the bottom of the parent class to achieve the purpose of coupling.
Polymorphism: polymorphism realizes the personalization of the method. Different subclasses can implement different methods according to the specific situation. The method defined by the parent class is not flexible enough to meet special situations
This article comes from: www.cnblogs.com/zhengyufeng…