2012-02-07 109 views
1

這很可能是簡單的,但即時通訊掙扎一點是有可能做到像下面這樣的事情,因爲它沒有在第二個函數中提醒。在另一個函數中使用一個函數的變量 - 想想我錯過了什麼

function func1() { 
    var test = 5; 
    var testAgain = 8; 
} 


function func3() { 
    func1(); 
    alert(test); 
} 

編輯:

謝謝大家對您的意見,但我似乎無法得到它的工作。

我下面就是我試圖讓這個在

工作似乎並不想從setImageReelWidth通過增值經銷商,只是提醒0時,它得到旋轉=功能()或$代碼(「.paging一」)。點擊(函數()。所以我點擊它提醒4(這是正確的),然後當它再次兩次運行它,我只是得到0

var divWidth = 0; 
var slideTotal = 0; 
var divReelWidth = 0; 


function setImageReelWidth() { 
    var divWidth = $(".window").width(); 
    var slideTotal = $(".slide").size(); 
    var divReelWidth = divWidth * slideTotal; 
    alert(slideTotal); 
    //Adjust the image reel to its new size 
    $(".image_reel").css({ 'width': divReelWidth }); 
} 





$(document).ready(function() { 
    //////////////////////////// INITAL SET UP ///////////////////////////////////////////// 

    //Get size of images, how many there are, then determin the size of the image reel. 
    //var divWidth = $(".window").width(); 
    //var slideTotal = $(".slide").size(); 
    //var divReelWidth = divWidth * slideTotal; 

    //Adjust the image reel to its new size 
    //$(".image_reel").css({ 'width': divReelWidth }); 

    //set the initial not active state 
    $('#prev').attr("class", "not_active"); 

    //////////////////////////// SLIDER ///////////////////////////////////////////// 

    //Paging + Slider Function 
    rotate = function() { 
     setImageReelWidth(); 
     alert(slideTotal); 
     var triggerID = $slideNumber - 1; //Get number of times to slide 
     var image_reelPosition = triggerID * divWidth; //Determines the distance the image reel needs to slide 
     //sets the active on the next and prev 
     if ($slideNumber == 1) { 
      $('#prev').attr("class", "not_active") 
      $('#next').attr("class", "active") 
     } 
     else if ($slideNumber == slideTotal) { 
      $('#next').attr("class", "not_active"); 
      $('#prev').attr("class", "active"); 
     } 
     else { 
      $('#prev').attr("class", "active") 
      $('#next').attr("class", "active") 
     }; 
     //Slider Animation 
     $(".image_reel").animate({ 
      left: -image_reelPosition 
     }, 500); 
    }; 

    //////////////////////////// SLIDER CALLS ///////////////////////////////////////////// 

    //click on numbers 
    $(".paging a").click(function() { 
     setImageReelWidth(); 
     alert(slideTotal); 
     setImageReelWidth(); 
     $active = $(this); //Activate the clicked paging 
     $slideNumber = $active.attr("rel"); 
     rotate(); //Trigger rotation immediately 
     return false; //Prevent browser jump to link anchor 
    }); 
+1

我想你想編寫alert(test);'not'alert('test');',因爲使用String'test'的警報不使用你的變量。但是,這確實可以解決您的問題,請參閱下面的答案。 – 2012-02-07 10:16:37

+1

在函數中使用'var'關鍵字時,它將創建一個新的局部變量,而不是使用全局變量。只要從函數內部刪除'var'字。 – JJJ 2012-02-07 11:36:19

+0

完美多數民衆贊成在我做錯了 – odd 2012-02-07 12:14:31

回答

4

您需要聲明全球範圍內變量。

var test = 0; 

function func1() { 
    test = 5; 
} 


function func3() { 
    func1(); 
    alert(test); 
} 

進一步閱讀上variable scope in javascript

+0

想再試一次嗎? ;)...做得好 – musefan 2012-02-07 10:14:21

+0

增加了更多的代碼,因爲我不能讓它工作 – odd 2012-02-07 11:32:19

6

使變量可用於兩個methos,通過全局定義它,就像Rory建議在his answer,是好的。

另一種解決方案是從第一功能回報它,並用它在第二個這樣的:

function func1() { 
    var test = 5; 
    return test; 
} 


function func3() { 
    var x = func1(); 
    alert(x); 
} 

編輯 另一種方式來做到這一點,是寫一個工廠函數來創建FUNC3:

var func3 = (function(){ 

    var test; 

    function func1() { 
     test = 5; 
    } 

    return function() { 
     func1(); 
     alert(test); 
    } 


})(); 

這裏發生的是以下幾點: 我創建了一個功能,那被馬上執行:

(function(){  
    // ...   
})(); 

在那裏,我有自己的變量test和另一個函數調用func1返回功能是被綁定到變量func3func3現在有權訪問var test以及func1但是該工廠外面沒有任何東西函數我寫可以訪問它們

看看我創建的小提琴:enter link description here

這樣的東西起初有點難理解,但它確實是JavaScript(IMHO)的力量。

+0

+1這是一個比我的全局變量更好的方法。 – 2012-02-07 10:16:11

+0

那麼,問題是什麼sluap試圖實現。我們需要看到更大的圖景來決定什麼適合這裏。你不應該刪除你的答案。此外,有兩種方法可以在兩個函數中擁有一個變量,而不需要在全局空間中擁有它們。 – 2012-02-07 10:21:54

+0

謝謝你們倆的意見,我已經改變了我的代碼,可以說我只是想拉出測試變種,第一個功能有幾個變種設置 – odd 2012-02-07 10:23:05

相關問題