1.js inherited implementation

Original Excerpt: https://www.cnblogs.com/gwf93/p/10384352.html

The first to write a parent class Car

function Car(name){
    this.name = name;
    this.driver = function (){
        console.log ( 'traveling thousands of miles' )
    }
}
Car.prototype.addOil = function(param){
    this.oil = param;
   the console.log ( the this .name + 'daily refueling' param + + 'L' )
}

(A) prototype inheritance chain:

   Core: the instance of the parent class as a prototype subclass

          instanceOf: a test object is an instance of a class

// prototype inheritance 
function baoma () {
}
BaoMa.prototype = new new key Car () // instance of the parent class as a prototype subclass inherits
BaoMa.prototype.name = "BMW"

var baoMa1 = new new baoma (); // instantiate subclass
console.log(baoMa1.name)
console.log(baoMa1.addOil(20))
console.log(baoMa1.driver())
console.log(baoMa1 instanceof Car)  // true
console.log(baoMa1 instanceof Car) // true
  1. Features:

    1. Pure inheritance, instance is an instance of a subclass, as well as instances of the parent class
    2. Parent new prototyping / prototype property, the subclass can access to
    3. Simple, easy to implement

    Disadvantages:

    1. Cat constructor may be increased for the instance attributes Cat instances. If you want to add the prototype properties and methods, it must be placed new Animal()executed after such statements.
    2. Multiple inheritance can not be achieved
    3. All properties from the prototype object is shared by all instances
    4. When you create a subclass instance, can not pass parameters to the parent class constructor

(B) constructor Inheritance: 

// constructor inherited 
function dazhong (name) {
    Car.call(this);
    this.name = name || '迈腾';
}
maiTeng the let = new new dazhong ();
console.log(maiTeng.name) // 迈腾
console.log(maiTeng.driver())
console.log(maiTeng instanceof Car); // false
console.log(maiTeng instanceof DaZhong) // true

Features:

  1. Solved, the subclass instances share issue 1 properties of the parent class reference
  2. When subclassed instance, you can pass parameters to parent
  3. Can implement multiple inheritance (call more than one parent class object)

Disadvantages:

  1. Examples of instances is not the parent class, the only instance of a subclass
  2. Examples can inherit attributes and methods of the parent class, the prototype can not inherit properties / methods
  3. Multiplexing function can not be achieved, a copy of each sub-class has a parent class instance functions affect performance

(C) class inheritance

In ES6, we can use  class to achieve inheritance, and is simple to implement

Core: Use  extends show inherited from which parent, and must be called in the subclass constructor  super, this code can be seen as Animal.call(this, name)

    class的本质就是函数

class DaZhong extends Car {
    constructor(name){
    super(name);
       this.name= ame || '迈腾';
    }
}
maiTeng the let = new new dazhong ();
console.log (maiTeng.name) // Magotan 
console.log (maiTeng.addOil (30))   // Magotan day refueling 30L 
console.log (maiTeng.driver ())
console.log(maiTeng instanceof Car); //true
console.log(maiTeng instanceof DaZhong) // true

 

Guess you like

Origin www.cnblogs.com/linjiu0505/p/11847815.html