2016-02-16 37 views
-4

我有一個問題,我的編碼請檢查下面的代碼jQuery函數互爲作用

var sliderFunction = { 
    init: function(sliderSettings) { 
    sliderFunction.sliderSettings(); 
    }, 
    sliderSettings: function() { 
    var sliderDiv = $('.slider'); 
    var ulDiv = $('.slider ul'); 
    var liDiv = $('.slider ul li'); 

    var sliderWidth = sliderDiv.width(); 
    var countLi = liDiv.length; 
    var ulTotalWidth = sliderWidth * countLi; 

    ulDiv.outerWidth(ulTotalWidth); 
    }, 
    nextContent: function() { 
    } 
} 

sliderFunction.init(); 

我想在nextContent功能sliderSettings功能的訪問var。我怎麼能提前

+0

你不能,範圍的問題。 – Jai

+0

抱歉無法取得Jai – reachtokish

回答

1

做到這一點

由於在全球範圍內定義他們,他們會訪問anywhwere

var sliderDiv,ulDiv,liDiv,sliderWidth,countLi,ulTotalWidth,ulDiv; 
var sliderFunction = { 
    init: function(sliderSettings) { 
    sliderFunction.sliderSettings(); 
}, 
sliderSettings: function() { 
sliderDiv = $('.slider'); 
ulDiv = $('.slider ul'); 
liDiv = $('.slider ul li'); 

sliderWidth = sliderDiv.width(); 
countLi = liDiv.length; 
ulTotalWidth = sliderWidth * countLi; 

ulDiv.outerWidth(ulTotalWidth); 
}, 

nextContent:function(){ 
    console.log(sliderDiv,ulDiv,liDiv,sliderWidth,countLi,ulTotalWidth,ulDiv); 
} 
}