码迷,mamicode.com
首页 > 其他好文 > 详细

对象冒充 实现多继承

时间:2017-05-07 13:02:42      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:name   this   console   strong   font   code   支持   机制   org   

 

注意:原型链时不支持多继承的

// 对象冒充实现类的多重继承
var ClassA=function (color){
    this.color = color;
    this.sayColor = function(){
        console.log(this.color);
    }
};
var ClassB=function (name){
    this.name = name;
    this.sayName = function(){
        console.log(this.name);
    }
};

//C 同时继承 A和 B
var ClassC=function(color,name){ //把ClassA作为常规函数来建立继承机制,而不是作为构造函数 ClassA.call(this,color); ClassB.call(this,name); }; var objA=new ClassA(‘red‘), objB=new ClassB(‘George‘), objC=new ClassC(‘blue‘,‘Nicholas‘); objA.sayColor(); //red objB.sayName(); //George objC.sayColor(); //blue objC.sayName(); //Nicholas

 

对象冒充 实现多继承

标签:name   this   console   strong   font   code   支持   机制   org   

原文地址:http://www.cnblogs.com/web-fusheng/p/6820332.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!