js prototype chain of understanding (4) - classic inheritance

Inheritance is a combination of classical inheritance, and that the advantages of the constructor prototype chain combinations mixed inheritance.

1. Avoid reference type attribute initialization

2. Avoid using the same method of initializing multiple times

    function Super(name){
        this.ages = [100,200,300];
        this.name = name;
    }
    Super.prototype.print = function(){
        console.log(this.ages);
    }

    function Sub(name){
        Super.call(this,name);
    }
    Sub.prototype = new Super();
    Sub.prototype.getName = function(){
        console.log("getName");
    }
    
    var subobj = new Sub('subobj');

 

Guess you like

Origin www.cnblogs.com/chenyi4/p/11988900.html