JS继承有哪些方式

205 阅读1分钟
  • class继承(es6)
    class Parent{
        constructor(){
          this.age = 19;
        }
    }
    class Child extends Parent{
	constructor(){
		super();
		this.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.run === o2.run );
  • 借用构造函数的形式继承
    function Parent(){
	this.age = 19;
	this.run = function(){}
    }

    function Child(){
            Parent.call(this);
            this.name = '张三'
    }

    var o1 = new Child();
    var o2 = new Child();
    console.log(  o1.run === o2.run );