当前位置:编程学习 > JS >>

简单的JS多重继承示例


$defined = function (v) {
    return 
!= undefined;
}

Class = function () {
    var 
base = {};
    for (var 
k=0k<arguments.lengthk++) {
    
//{{new arguments[k]() with custom constructor field.
        
var arguments[k].prototype;
        
o.constructor arguments[k];
        
arguments[k].call(o);
    
//}}
        
for (key in obase[key] = o[key];
    }
    function 
Klass () {
        
// for every class one object cache.
        
var clso null;
        function 
klass() {
            if (
arguments.length<=&& clso!=null) {
                
// hit cache.
                
return clso;
            }
            if (
$defined(this.constructor.init)) {
                
// use init() for class initialization.
                
this.constructor.init.apply(thisarguments);
            }
            
clso this;
        }
        
klass.prototype base;
        return 
klass;
    }
    return 
Klass();
}

= new Class();
A.init = function () {
    
this.400;
    
this.300;
}
= new Class(A);
B.init = function () {
    
this.200;
    
this.100;
}
= new Class(B);
C.init = function () {
    
this.0;
}
= new C();
alert(c.x);
alert(c.y);
alert(c.z); 

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,