Original
The original Book of Jane: https://www.jianshu.com/p/78ce11762f39
Outline
Objective
1. Prototype chain inheritance
2. Implement inheritance by borrowing a constructor function
3. Combination Mode inheritance
4. prototype-Type Inheritance
5. Parasitic inheritance
6. Parasitic combined inheritance
7. Code Resources
Objective
Inheritance is one of the most talked about concepts in oo language. Many OO languages support two ways of inheriting: interface inheritance and implementation inheritance. Interface inheritance inherits only the method signature, while implementing inheritance inherits the actual method. As mentioned earlier, because the function is not signed, interface inheritance cannot be implemented in ECMAScript. ECMAScript only supports implementation of inheritance, and its implementation is mainly based on the prototype chain to achieve.
1, the prototype chain inherits the prototype chain is the main method to realize the inheritance.
The basic idea of using a prototype chain to inherit is to have a reference type inherit the properties and methods of another reference type.
function Supertype (name) { this.name = name;} SuperType.prototype.sayName = function () { return this.name;}; Function subtype (age) { this.age = age;} Main code subtype.prototype = new Supertype (' KK '); SubType.prototype.sayAge = function () { return this.age;}; var instance = new subtype (n); Console.log (instance);
2, using the constructor to implement inheritance borrowing constructor implementation of the implementation of the idea: Call the superclass constructor inside the subtype constructor.
function Supertype (name) { this.name = name; This.sayname = function () { return this.name; }} Function subtype (name,age) { this.age = age; This.sayage = function () { return this.age; } Supertype.call (this,name);} var instance = new Subtype (' KK ', 2); Console.log (instance);
3, combination mode inheritance combination inheritance, also known as Perjury classic inheritance, refers to the prototype chain and borrowed the technology of the constructor into a piece.
Idea: Using the prototype chain to implement the inheritance of the prototype properties and methods, by borrowing the constructor to implement the inheritance of the instance properties.
function Supertype (name) { this.name = name;} SuperType.prototype.sayName = function () { console.log (this.name);}; Function subtype (name,age) { //Inherit attribute Supertype.call (this,name); This.age = age;} Inheritance method Subtype.prototype = new Supertype (' GG '); SubType.prototype.sayAge = function () { console.log (this.age);} var instance = new Subtype ("KK"), Console.log (instance);
4. Prototype inheritance: The method does not use a strict constructor, and the idea is that the prototype can create new objects based on existing objects without having to create custom types.
function Object (o) { function F () {} f.prototype = O; return new F (); var person = { name: ' KK ', age:12};var Extendperson = object (person); Console.log (Extendperson);
5. The idea of parasitic inheritance: Create a function that encapsulates the inheritance process, which internally enhances the object in some way, and finally returns the object as if it did all the work.
function Object (o) { function F () {} f.prototype = O; return new F (); function Createanother (original) { var clone = object (original);//Create a new object by calling function Clone.sayhi = function () {// In some way to enhance this object console.log ("Hi"); }; Return clone;//returns this object}var person = { name: ' KK ', age:13};var Anotherperson = createanother (person); Console.log (Anotherperson);
6, parasitic combined inheritance of so-called parasitic combined inheritance: that is, by borrowing the constructor to inherit attributes, through the prototype chain of the hybrid form of inheritance methods.
The basic idea behind it is that you don't have to call a super-type constructor to specify a prototype of a subtype, all we need is a copy of the super-type prototype. Essentially, a parasitic inheritance is used to inherit a super-type prototype, and then the result is assigned to the child type's prototype.
function Object (o) { function F () {} f.prototype = O; return new F (); function Inheritprototype (subtype,supertype) { var prototype = object (supertype.prototype);//Create Object Prototype.constructor = subtype;//Enhanced object Subtype.prototype = prototype;//specified object}function supertype (name) { THIS.name = name;} SuperType.prototype.sayName = function () { console.log (this.name);}; Function subtype (name,age) { //Inherit attribute Supertype.call (this,name); This.age = age;} Inheritprototype (Subtype,supertype); SubType.prototype.sayAge = function () { console.log (this.age);}; var instance = new Subtype (' KK ', ' K '); Console.log (instance);
7. Code Resources
The extendobject.js in JavaScript instance code contains the code for this blog, which contains several basic ways of inheriting JavaScript objects, and hopefully it will help readers.
The inheritance of JavaScript objects