zoukankan      html  css  js  c++  java
  • JavaScript 继承

    继承到目前为止我所掌握的总共有六种,下边的是所举的例子(A为父类,B为子类)

    1. 原型继承:将父类的实例赋值给子类的原型

     举个例子:

    function A(){

      this.name = 'cc'

    }

    A.prototype.x = 56;

    function B(){

      this.age = 19

    }

    B.prototype = new A;

    var a =new A;

    var    b=new B;

    这就是原型继承 将父类的私有和公有都继承在子类的原型上,成为子类的公有属性。

     

    2.call继承:将父类私有的继承为子类私有的

    function A(){

      this.name = 'cc'

    }

    A.prototype.x = 56;

    function B(){

      this.age = 19

      A.call(this)

    }

    3.冒充对象继承:将父类的私有和公有继承到子类私有

    function A(){

      this.name = 'cc'

    }

    A.prototype.x = 56;

    function B(){

      this.age = 19

      var temp = new A;

      for(var key in temp){

        this[key] = temp[k]

      }

      temp = null;

    }

    var a =new A;

    var    b=new B;

     

     4.混合继承: 私有继承私有,公有继承私有和公有(此处的私有一共继承了两遍) 

     混合继承是call和原型继承的结合

     

     

    function A(){

      this.name = 'cc'

    }

    A.prototype.x = 56;

    function B(){

      this.age = 19

      A.call(this)

    }

    B.prototype = new A;

    5. 组合继承 私有的继承为私有的 公有的继承为公有的

    function A(){

      this.name = 'cc'

    }

    A.prototype.x = 56;

    function B(){

      this.age = 19

      A.call(this)

    }

    B.prototype = Object.create;

    var a =new A;

    var    b=new B;

    6.中间类继承

    function f(){

    arguments.__proto__ = Array.prototype;

    arguments.shift()

    }

    f(12,26,39)

    arguments 不是一个数组,没有array的那些自带的方法,现在我们想argumentsarray的那些方法,将arguments的原型执行Array内置类的原型。

     

  • 相关阅读:
    eclipse fail to create java virtual machine
    sas软件连接Oracle数据库的办法
    JAVA中数据的读取与写入,不同类型数据的转换
    二维数组的在函数中的传递
    【转】值传递与引用传递
    【转】深拷贝与浅拷贝
    (转)JS报表控件highcharts应用
    highstocks.js使用指南
    (转)Highcharts使用指南(出处:http://liuhaorain.cnblogs.com )
    jQuery实现checkbox全选,反选
  • 原文地址:https://www.cnblogs.com/blankOne/p/10235071.html
Copyright © 2011-2022 走看看