zoukankan      html  css  js  c++  java
  • javascript继承(对象冒充的多重继承)

    function ClassA(color){
        this.color = color;
        this.sayColor = function(){
            console.log(this.color);    
        }
    }
    
    
    
    function ClassB(name){
        this.name = name;
        this.sayName = function(){
            console.log(this.name)
        }
    }
    
    
    //通过对ClassA、ClassB使用CALL方法,改变其THIS的指向,让其this指向被实例化的C这个新对象,从而让C的实例能够创建出A和B的方法和属性
    function ClassC(price,scolor,sname){
        ClassA.call(this,scolor);  //实现继承
        ClassB.call(this,sname);  //实现继承

       this.price = price; this.price = function(){ console.log(this.price); } } var oC = new ClassC(1200,"red","anyCall"); oC.price(); oC.sayColor(); oC.sayName();

    通过对ClassA、ClassB使用CALL方法,改变其THIS的指向,让其this指向被实例化的C这个新对象,从而让C的实例能够创建出A和B的方法和属性(APPLY方法和CALL方法,只是后面传的参数有区别)

  • 相关阅读:
    iBatis,第二次亲密接触
    微斯人,吾谁与归
    一个月了
    生命在于运动
    眼皮跳了好几天
    往返
    中病毒,学习批处理

    爱如潮水
    今天夏至
  • 原文地址:https://www.cnblogs.com/lufy/p/2521156.html
Copyright © 2011-2022 走看看