西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

首頁編程開發(fā)javascript|JQuery → javascript面向?qū)ο蟀b類Class的類庫解析

javascript面向?qū)ο蟀b類Class的類庫解析

相關軟件相關文章發(fā)表評論 來源:pigtail時間:2013/1/22 14:42:29字體大小:A-A+

作者:穆乙點擊:0次評論:0次標簽: 面向?qū)ο?/a>

3 頁 簡單實現(xiàn)

三、還有一種簡單實現(xiàn)

實現(xiàn)思路很簡單,就是利用ECMAScript 5 原型式繼承Object.create方法,封裝成一個方法,如果不支持ECMAScript5的環(huán)境,就平移退化到

function F() {};  
F.prototype = superCtor.prototype;  
ctor.prototype = new F();  
ctor.prototype.constructor = ctor;

同樣的,除最后一個參數(shù)是當前類的方法聲明,其它參數(shù)均做為繼承父類,需要循環(huán)繼承,但當這里處理的相對比較簡單,沒涉及到覆蓋。你可以自己動手添加。

var Class = (function() {  
      
    /**  
     * Inherits function.(node.js)  
     *   
     * @param ctor subclass's constructor.  
     * @param superctor superclass's constructor.  
     */  
    var inherits = function(ctor, superCtor) { 
        // 顯式的指定父類
        ctor.super_ = superCtor;  
          
        // ECMAScript 5  原型式繼承并解除引用
        if (Object.create) {  
            ctor.prototype = Object.create(superCtor.prototype, {  
                constructor: {  
                    value: ctor,  
                    enumerable: false,  
                    writable: true,  
                    configurable: true  
                }  
            });  
        } else {  
            // 無Object.create方法的平穩(wěn)退化
            function F() {};  
            F.prototype = superCtor.prototype;  
            ctor.prototype = new F();  
            ctor.prototype.constructor = ctor;  
        }  
    };  
      
    /**  
     * Class function.  
     */  
    return function() {  
        // 最后一個參數(shù)是新類方法、屬性和構(gòu)造函數(shù)聲明
        var subClazz = arguments[arguments.length - 1] || function() {};  
        // initialize是構(gòu)造函數(shù),否構(gòu)造函數(shù)就是一個空函數(shù)
        var fn = subClazz.initialize == null ? function() {} : subClazz.initialize;  
        // 繼承除最一個參數(shù)以的類,多繼承,也可以用作擴展方法 
        for (var index = 0; index < arguments.length - 1; index++) {  
            inherits(fn, arguments[index]);  
        }  
        // 實現(xiàn)新類的方法
        for (var prop in subClazz) {  
              
            if (prop == "initialize") {  
                continue;  
            }  
              
            fn.prototype[prop] = subClazz[prop];  
        }  
        
        return fn;  
    }  
      
})(); 

看下面實例:

/**  
 * The definition of Cat Class.  
 */  
var Cat = Class({  
      
    /**  
     * Constructor.  
     *   
     * @param name Cat's name  
     */  
    initialize: function(name) {
        this.name = name;  
    },  
      
    /**  
     * Eat function.  
     */  
    eat: function() {  
        alert(this.name + " is eating fish.");  
    }  
});  
  
/**  
 * The definition of Black Cat Class.  
 */  
var BlackCat = Class(Cat, {  
      
    /**  
     * Constructor.  
     *   
     * @param name Cat's name.  
     * @param age Cat's age.  
     */  
    initialize: function(name, age) {  
        // call the constructor of super class.  
        BlackCat.super_.call(this, name);  
        this.age = age;  
          
    },  
      
    /**  
     * Eat function.  
     */  
    eat: function() {  
        alert(this.name + "(" + this.age + ") is eating dog.");  
    }  
});  
  
/**  
 * The definition of Black Fat Cat Class.  
 */  
var BlackFatCat = Class(BlackCat, {  
      
    /**  
     * Constructor.  
     *   
     * @param name Cat's name.  
     * @param age Cat's age.  
     * @param weight Cat's weight.  
     */  
    initialize: function(name, age, weight) {  
        // call the constructor of super class.  
        BlackFatCat.super_.call(this, name, age);  
        this.weight = weight;  
    },  
      
    /**  
     * Eat function.  
     */  
    eat: function() {  
        alert(this.name + "(" + this.age + ") is eating dog. My weight: " + this.weight);  
    }  
});  
  
  
/**  
 * The definition of Dog Class.  
 */  
var Dog = Class({});  
  
var cat = new BlackFatCat("John", 24, "100kg");  
cat.eat();  
  
// true  
alert(cat instanceof Cat);  
  
// true  
alert(cat instanceof BlackCat);  
  
// true  
alert(cat instanceof BlackFatCat);  
  
// true  
alert(cat.constructor === BlackFatCat);  
  
// false  
alert(cat instanceof Dog);  
              
              

        
    推薦文章

    沒有數(shù)據(jù)