Implement a parent class

/ / define a Animal function Animal (name) {/ / attribute this. Name = name | | 'Animal'; // Instance method this.sleep = function(){console.log(this.name + 'sleeping! '); Eat = function(food) {console.log(this.name + 'eating:' + food); };

Archetypal chain inheritance

Core: Use an instance of the parent class as the prototype Object.create for the child class

function Cat(){ 
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.eat('fish'));
console.log(cat.sleep());
console.log(cat instanceof Animal); //true 
console.log(cat instanceof Cat); //true

The characteristics of

1. A very pure inheritance relationship. Instances are instances of subclasses as well as instances of superclasses. The parent class adds the prototype method/prototype property, and the child class can access it. 3. Simple and easy to implement

disadvantages

1. If you want to add new prototype properties and methods, you must execute them after a statement like new Animal(). 2. 3. All properties from the prototype object are shared by all instances 4. When an instance of a subclass is created, arguments cannot be passed to the constructor of the superclass

Recommended index: ★★ (3, 4 two fatal defects)

Tectonic inheritance

Core: Using the constructor of the superclass to enhance the instance of the subclass copies the instance properties of the superclass to the subclass (no prototype)

function Cat(name){
  Animal.call(this);
  this.name = name || 'Tom';
}

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true

Features:

2. When creating an instance of a subclass, you can pass parameters to the superclass. 3. Can implement multiple inheritance (call multiple parent objects)

Disadvantages:

1. Instance is not an instance of the parent class, but an instance of the child class. 2. Function reuse cannot be realized. Each subclass has a copy of the instance function of the parent class, which affects performance

Recommended index: ★★ (disadvantages 3)

Examples of inheritance

Core: Adds a new feature to the parent instance and returns it as a child instance

function Cat(name){
  var instance = new Animal();
  instance.name = name || 'Tom';
  return instance;
}

// Test Code
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // false

Features:

1. There is no restriction on how to call it. Whether it is a new subclass () or a subclass (), the returned object has the same effect

Disadvantages:

2. Multiple inheritance is not supported. 2. Multiple inheritance is not supported

Recommended index: ★★

Combination of inheritance

Core: Inherits the properties of the parent class and retains the benefits of parameter passing by calling the parent class construct, and then realizes function reuse by using the parent class instance as the prototype of the subclass

function Cat(name){ Animal.call(this); this.name = name || 'Tom'; } Cat.prototype = new Animal(); // Composite inheritance also needs to be fixed by the constructor, thanks to @Learning Unlimited C. Cat.prototype.constructor = Cat; // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); // true

Features:

1. Compensates for the defect of method 2 by inheriting both instance properties/methods and prototype properties/methods 2. Is both an instance of the subclass and an instance of the parent class 3. There is no problem of sharing reference attributes 4. 5. Functions can be reused

Disadvantages:

1. The superclass constructor is called twice, generating two instances (subclass instances mask the one on the subclass prototype)

Recommended index: ★★★★ ★ (just a little more memory consumption)

Parasitic combinatorial inheritance

Core: In a parasitic way, the instance properties of the parent class are cut off. In this way, when the constructor of the parent class is called twice, the two instance methods/properties are not initialized, avoiding the drawback of composite inheritance

function Cat(name){ Animal.call(this); this.name = name || 'Tom'; } (function(){var Super = function(){// create a class with no instance method var Super = function(){}; Super.prototype = Animal.prototype; Cat.prototype = new Super(); cat. prototype = new Super(); }) (); // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); //true Cat.prototype.constructor = Cat; // The following constructor needs to be fixed

Features:

perfect

Disadvantages:

The implementation is complicated.

Recommended index: ★★★★ (to achieve complex, deduct one star)

extension

Object.create()

The object.create () method creates a new Object, using the existing Object to provide the __proto__ of the newly created Object

Object. The create (proto, [propertiesObject])

parameter

Proto is the prototype object for the newly created object. PropertiesObject optional. You need to pass in an Object whose property type refers to the second argument of Object.defineProperties(). If the parameter is specified and not undefined, the incoming object’s own enumerable properties (that is, its own defined properties, rather than the enumerated properties on its prototype chain) will add the specified property value and the corresponding property descriptor to the newly created object.

The return value

A new object with the specified prototype object and properties.

Manual implementation

function _create(obj){
  function F(){
     
  }
  F.prototype = obj
  return new F()
}