JavaScriipt ES6 - 类的本质,ES6之前的实现继承

1.类的本质

1.class 本质上还是funcation

2.类中所有的方法都定义在原型对象上prototype

3.类创建的实例也可以通过_ _proro_ _ 访问原型对象

 

function.call(Object,value...) 可以改变function的指向,且调用函数

利用call函数继承属性:

  <script>
    //父类
    function Father(name,age){
      this.name=name
      this.age=age
    }
    //子类
    function Son(){
      //father的this被修改为了当前类
      Father.call(this,"levi",18)
    }
    var son=new Son()
  </script>

利用原型对象继承方法:

  <script>
    //父类
    function Father(name,age){

    }
    Father.prototype.sum=function (a,b){
      console.log(a+b)
    }
    //子类
    Son.prototype=new Father()
    Son.prototype.constructor=Son;
    function Son(){

    }
    var son=new Son()
    son.sum(1,2)
  </script>

posted on 2022-05-17 16:29  每天积极向上  阅读(37)  评论(0编辑  收藏  举报

导航