Algorithm-javascript self-Implementation inheritance extend (single-Inheritance prototype object and single-inheritance constructor)

Source: Internet
Author: User

#  Self-implementing inheritance (one-time inheritance constructor)  //the constructor of the parent class   function father (name,age) {   this.name= name   this.age=age  }    //the prototype object of the parent class   father.prototype={    constructor:father,   say:function () {    console.log (' I  am  ' +this.name+ '  , ' +this.age+ '  years old ')    }  }     //class Inheritance: Inherits only the parent class template, does not inherit the prototype object (inherits from the method implementation of the constructor)     //the constructor of the subclass   function  child (Name,age) {   //call apply  Extended Parent template usage range     Child.superClass.constructor.call (this,name,age)   }  function extend (sub,sup) {    //initialization: Create empty function, empty template and empty prototype Object    var f=new function ()    // Empty function: Inherit only prototype object of parent class    f.prototype=sup.prototype   //subclass inherits empty function, can implement inherit only prototype object, do not inherit constructor     sub.prototype=new f ()    //also primitive constructors for atomic classes    sub.prototype.constructor=sub      // Save the parent prototype object, role 1: Decoupling, Function 2: Easy to get the parent class prototype object    sub.superclass=sup.prototype     }     extend (Child,father)   var c1=new child (' li4 ', +)   c1.say ()     console.log (Father.prototype.isPrototypeOf (C1))


Algorithm-javascript self-Implementation inheritance extend (single-Inheritance prototype object and single-inheritance constructor)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.