继承

继承

  ES6继承 class类

class Parent{

    constructor(){

    this.name='父亲'

    }

    Run(){

        Console.log(这里是 Parent.prototype.run(){ })

    }

}

class Children extends Parent{

    constructor(){

        super();

        this.title='儿子'

    }

}

 

var p1 = new Children()

console.log(p1.name)  //父亲,就可以继承了                

原型链继承

function Parent(){

    this.age = 19;

    this.run = function(){}

}

 

function Child(){

    this.name = '张三'

}

//原型链

Child.prototype = new Parent();

var o1 = new Child();

var o2 = new Child();

 

console.log(  o1.age  ); 19

console.log(  o1.run === o2.run ); true

用call继承

function Parent(){

    this.age = 18;

    this.run = function(){}

}

function Child(){

    Parent.call(this);

    this.name = '张三'

}

 

var o1 = new Child();

var o2 = new Child();

console.log(  o1.age  );  18

console.log(  o1.run === o2.run );  false

 

posted @ 2022-05-11 16:05  杨建鑫  阅读(20)  评论(0编辑  收藏  举报