Home Web Front-end JS Tutorial Detailed explanation of the use of parasitic combined inheritance in js

Detailed explanation of the use of parasitic combined inheritance in js

Apr 18, 2018 pm 04:41 PM
javascript Detailed explanation

This time I will bring you js parasitic combined type inheritance Detailed explanation of use, what are the precautions for using js parasitic combined type inheritance, the following is a practical case, one Get up and take a look.

Combined inheritance:

function Person( uName ){
      this.skills = [ 'php', 'javascript' ];
      this.userName = uName;
    }
    Person.prototype.showUserName = function(){
      return this.userName;
    }
    function Teacher ( uName ){
      Person.call( this, uName );
    }
    Teacher.prototype = new Person();
    var oT1 = new Teacher( 'ghostwu' );
    oT1.skills.push( 'linux' );
    var oT2 = new Teacher( 'ghostwu' );
    console.log( oT2.skills ); //php,javascript
    console.log( oT2.showUserName() ); //ghostwu
Copy after login

There is a disadvantage of combined inheritance. The constructor of the parent class will be called twice.

Line 11, sets the subclass prototype object (prototype), and calls the first

Line 9, when instantiating the object, call

again The purpose of the constructor is to copy attributes. Line 9 is definitely indispensable. The purpose of line 11 is to obtain the method on the parent class prototype object (prototype). Based on this purpose, is there any Other methods

Can it be done? Can we get the methods on the prototype object of the parent class without instantiating the parent class constructor? Of course, we can use parasitic inheritance to get the methods on the parent class prototype object

function Person( uName ){
      this.skills = [ 'php', 'javascript' ];
      this.userName = uName;
    }
    Person.prototype.showUserName = function(){
      return this.userName;
    }
    function Teacher ( uName ){
      Person.call( this, uName );
    }
    function object( o ){
      var G = function(){};
      G.prototype = o;
      return new G();
    }
    function inheritPrototype( subObj, superObj ){
      var proObj = object( superObj.prototype ); //复制父类superObj的原型对象
      proObj.constructor = subObj; //constructor指向子类构造函数
      subObj.prototype = proObj; //再把这个对象给子类的原型对象
    }
    inheritPrototype( Teacher, Person );
    var oT1 = new Teacher( 'ghostwu' );
    oT1.skills.push( 'linux' );
    var oT2 = new Teacher( 'ghostwu' );
    console.log( oT2.skills ); //php,javascript
    console.log( oT2.showUserName() ); //ghostwu
Copy after login

In fact, to put it bluntly, parasitic combined inheritance is a prototype object that borrows a constructor and is equivalent to a shallow copy of the parent class

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website !

Recommended reading:



The above is the detailed content of Detailed explanation of the use of parasitic combined inheritance in js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Detailed explanation of obtaining administrator rights in Win11

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in Oracle SQL

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

Detailed explanation of the role and usage of PHP modulo operator

Detailed explanation of the linux system call system() function Detailed explanation of the linux system call system() function Feb 22, 2024 pm 08:21 PM

Detailed explanation of the linux system call system() function

Detailed analysis of C language learning route Detailed analysis of C language learning route Feb 18, 2024 am 10:38 AM

Detailed analysis of C language learning route

Detailed explanation of Linux curl command Detailed explanation of Linux curl command Feb 21, 2024 pm 10:33 PM

Detailed explanation of Linux curl command

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

Detailed explanation of numpy version query method Detailed explanation of numpy version query method Jan 19, 2024 am 08:20 AM

Detailed explanation of numpy version query method

See all articles