zoukankan      html  css  js  c++  java
  • 主要继承方式

    1.原型链继承

    基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法;

    function SuperType(){
      this.property = true;
    }
    SuperType.prototype.getSuperValue = function(){
      return this.property;
    };
    function SubType(){
      this.subproperty = false;
    }
    //继承了SuperType
    SubType.prototype = new SuperType();
    SubType.prototype.getSubValue = function (){
      return this.subproperty;
    };
    var instance = new SubType();
    alert(instance.getSuperValue()); //true

    2.构造函数

    基本思想:在子类型构造函数的内部调用超类(父类)型构造函数,通过使用apply()和call()方法可以在(将来)新创建的对象上执行构造函数

    每个函数都包含两个非继承而来的方法:apply()和call()。这两个方法的用途都是在特定的作用域中调用函数,实际上等于设置函数体内this 对象的值。

    apply()和call()的功能一致,只是传递参数的方式不同:

    apply(this,arguments)

    call(this,argument1,argument2,argument3,...)

    function SuperType(){
        this.colors = ["red", "blue", "green"];
    }
    function SubType(){
        //继承了SuperType
        SuperType.call(this);  // 设置superTpe函数体内this对象的值为subType函数
    }
    var instance1 = new SubType();
    instance1.colors.push("black");
    alert(instance1.colors); //"red,blue,green,black"
    var instance2 = new SubType();
    alert(instance2.colors); //"red,blue,green"
  • 相关阅读:
    Java并发编程(二)线程任务的中断(interrupt)
    Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)
    青蛙跳台阶(Fibonacci数列)
    旋转数组的最小值
    用两个栈实现队列
    重建二叉树
    二维数组中的查找
    Lab 3-1
    Lab 1-4
    Lab 1-3
  • 原文地址:https://www.cnblogs.com/yzg1/p/4894020.html
Copyright © 2011-2022 走看看