js basis - Inheritance

1, implementation inheritance: the prototype chain
        function extend1() {
          this.name = "John Doe";
        }
        function extend2(){
          this.age =18;
        }
        extend2.prototype = new extend1 (); // extend2 inherited attributes extend1
        var _extend1 = new extend2();
        console.log (_extend1.name); // Joe Smith
        console.log(_extend1.age);//18
  function extend3() {
          this.address = "Chongqing";
        }
        extend3.prototype = new extend2 (); // extend3 inherited extend1 and extend2
        var _extend2 = new extend3();
        console.log (_extend2.name); // Joe Smith
        console.log(_extend2.age);//18
        console.log (_extend2.address); // Chongqing
2, the combination of inheritance
  function Group1(age) {
          this.name = ["Linda",'Bob','Lucy','Anna'];
          this.age = 25;
        }
        Group1.prototype.run = function () {
          return this.name + ',' + this.age;
        }
        function Group2(age) {
          Group1.call (this, age); // object masquerading
        }
        Group2.prototype = new Group1 (); // prototype chain inheritance
        var _group1 = new Group2(20);
        console.log(_group1.run());

Guess you like

Origin www.cnblogs.com/LindaBlog/p/10984189.html