2016-09-15 97 views
0

我想傳遞一個數組,我已經保存爲var。我在父函數中聲明var,並在父函數中添加arr作爲參數。然後,我將arr作爲回調調用中的參數。 控制檯告訴我linksA是未定義的。從函數傳遞變量作爲參數

var supportLists = function(selector, arr) { 
    var parentList = document.getElementById(selector); 
    var theList = parentList.querySelectorAll("a"); 

    var linksA = [ 
     "http://www.example.com", 
     "http://www.example.com/path2", 
     "1", 
     "2", 
     "4", 
     "3", 
     "5", 
     "6", 
     "7" 
    ]; 

    var linksB = [ 
     "1", 
     "2", 
     "3" 
    ]; 

    var linksC = [ 
     "1", 
     "2", 
     "3", 
     "4", 
     "5", 
     "6", 
     "7", 
     "8", 
     "9", 
     "10", 
     "11", 
     "12" 
    ]; 

    var linksD = [ 
     "1", 
     "2" 
    ]; 

    var linksE = [ 
     "1", 
     "2", 
     "3" 
    ]; 

    var linksF = [ 
     "1", 
     "2", 
     "3", 
     "4", 
     "5", 
     "6" 
    ]; 

    var linksG = [ 
     "1", 
     "2", 
     "3" 
    ]; 

    var linksH = [ 
     "1", 
     "2", 
     "3", 
     "4" 
    ]; 

    var linksI = [ 
     "1" 
    ]; 

    var linksJ = [ 
     "1", 
     "2", 
     "3", 
     "4", 
     "5" 
    ]; 

    function processLi(listItems, links) { 

     for (var i = 0; i < links.length; i++) { 

      listItems.forEach(function(item, index) { 
       item.href = links[index]; 
       item.target = "_blank"; 

      }); 

     } 
    } 

    processLi(theList, arr); 
}; 

supportLists("support-list", linksA); 
supportLists("support-list-b", linksB); 
supportLists("support-list-c", linksC); 
supportLists("support-list-d", linksD); 
supportLists("support-list-e", linksE); 
supportLists("support-list-f", linksF); 
supportLists("support-list-g", linksG); 
supportLists("support-list-h", linksH); 
supportLists("support-list-i", linksI); 
supportLists("support-list-j", linksJ); 
+3

您試圖在聲明它們的函數外部使用那些'links'變量。 – 2016-09-15 17:13:43

+2

添加@squint答案,你可以聲明數組在相同的範圍上,函數將被調用並將它們作爲參數傳遞給函數,或者傳遞鏈接數組的名稱作爲輸入參數函數並在其中有一個邏輯來選擇和使用正確的數組。 – GCSDC

+0

如果我將它們放入processLi函數中,會得到相同的錯誤。 除此之外,我不是在調用中使用它們,而是坐在父函數內部,而不是回調? – ardev

回答

1

如果你想用它在你想使用它或將它傳遞(給一個函數)的範圍,這意味着它在該範圍或將被宣佈爲獲得一個變量一個父範圍。

由於您將數組傳遞給supportLists函數,因此您必須在該函數之外聲明它們。

如果移動功能以外的所有數組聲明您的代碼會是這個樣子(我增加了一些註解,以顯示其中一個範圍開始,在哪裏結束)

// This is the 'parent' scope (probably the global/window scope in your case) 

var linksA = [ 
    "http://www.example.com", 
    // ... 
]; 

// ... 

var linksJ = [ 
    "1", 
    "2", 
    "3", 
    "4", 
    "5" 
]; 

var supportLists = function(selector, arr) { 
    // Here begins the 'supportLists' function scope 
    // The 'supportLists' function has access to this scope and the 'parent' scope 
    var parentList = document.getElementById(selector); 
    var theList = parentList.querySelectorAll("a"); 

    function processLi(listItems, links) { 
     // Here begins the 'processLi' function scope 
     // The 'processLi' function has access to this scope, the 'supportLists' scope and the 'parent' scope 

     for (var i = 0; i < links.length; i++) { 

      listItems.forEach(function(item, index) { 
       // Here begins the 'function(item, index)' function scope 
       // The 'function(item, index)' function has access to this scope, the 'processLi' scope, the 'supportLists' scope and the 'parent' scope 

       item.href = links[index]; 
       item.target = "_blank"; 
      });// Here ends 'function(item, index)' function scope 
      // Back in the 'processLi' function scope 
     } 
    } // Here ends the 'processLi' function scope 
    // Back in the 'supportLists' function scope 

    processLi(theList, arr); 
}; // Here ends the 'supportLists' function scope 
// Back in the 'parent' scope 

supportLists("support-list", linksA); 
supportLists("support-list-b", linksB); 
supportLists("support-list-c", linksC); 
supportLists("support-list-d", linksD); 
supportLists("support-list-e", linksE); 
supportLists("support-list-f", linksF); 
supportLists("support-list-g", linksG); 
supportLists("support-list-h", linksH); 
supportLists("support-list-i", linksI); 
supportLists("support-list-j", linksJ); 
+0

感謝,這工作。只是爲了討論,不應該var的回調作用域的範圍,因爲變量是在回調應該繼承的父作用域內定義的嗎? – ardev

+0

@ardev:在你的原始代碼中定義了數組變量在'supportLists'函數範圍內(而不是'parent'/ global scope),但是你想在全局範圍內使用它們,這些數組變量是不可用/聲明的。 /stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Ma3x

0

目前,您的變量在本地範圍內定義,因此它們在supportLists函數外部不可見。

解決方案:

定義函數外部的變量。例如,

var linksB = ["1", "2", "3"]; 
var supportLists = function(selector, arr) { //YOUR CODE } 
+0

#2不是一個解決方案,你需要調用函數來創建變量在您將變量作爲參數調用之前,請先閱讀它們。而且,全局變量是邪惡的。 – Bergi

+0

@Bergi謝謝指出。 –