日期:2014-05-16  浏览次数:20307 次

JS 实现继承
<script type="text/javascript">
/** 继承用的功能函数 */
Player.extend = function (BaseFunc, config){
var F = function(){
/** 继承属性 */
BaseFunc.apply(this, arguments);

/** 赋新的属性 */
for(var key in config) {
this[key] = config[key];
}
}
   
/** 继承方法 */
F.prototype = BaseFunc.prototype;

/** 构造函数还是自己 */
F.prototype.constructor = F;
    
    return F;
}
/** 定义一个Animal类 **/
function Animal(name){
    this.name=name + "d"; 
}
Animal.prototype.oper = function(){alert("Animal")};
Animal.prototype.type = function(){alert("type")};


/** 定义一个Lion,继承自Animal**/
var Lion = Player.extend(Animal, {
oper:function(){alert("Lion");}
});

/** 测试类 */
var lion =  new Lion("d");
alert(lion.name);
lion.oper();
lion.type();
</script>