2012-06-26 23 views

回答

1

我剛剛調整了你的代碼

var CssLoader = (function(){ 
    var $=''; //the global $ inside CssLoader 
    function init($, cssPathArray){ // Receive the jquery in the first argument 
     $=jq; // assign jQuery to global $, so it'll be available inside CssLoader 
     alert($('body'));    
    } 

    return { 
     init : init 
    }; 
})(); 

而且內部函數調用mainCssLoader.init如下

CssLoader.init($, ['cssPath1', 'cssPath2']); // Pass the jquery in the first argument 

DEMO.

+0

謝謝。這將工作,但這將意味着我將不得不將jquery傳遞到我創建的每個對象(需要它)。我希望避免這種情況。 – Finnnn

+0

不,您應該首先調用init,以便它可以在全球範圍內使用,但這是正確的方法,我只是稍微調整了一下代碼。檢查演示,有兩個警報。不用謝! –

相關問題