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内置类的原型。

     

  • 相关阅读:
    Sysbench对Mysql进行基准测试
    yum安装软件时报错libmysqlclient.so.18()(64bit)
    redis启动报错Could not connect to Redis at 127.0.0.1:6379: 由于目标计算机积极拒绝,无法连接。
    cmd 中连接mysql时报'mysql'不是内部或外部命令,也不是可运行的程序或批处理文件
    Mysql远程连接权限
    Git基础教程
    电脑adb命令给智能电视安装APK
    python基础之类
    python基础之函数
    前端之CSS第二部分属性相关
  • 原文地址:https://www.cnblogs.com/blankOne/p/10235071.html
Copyright © 2011-2022 走看看