2016-10-11 21 views
0

我需要將多個textareas的值與對象的屬性相匹配。它應該只是一個比賽,如果:在不使用全局的情況下訪問另一個函數中的局部變量

  1. 正在使用的textarea的值等於對象中包含的
  2. 的人的名字,如果textarea的指數等於這個人的ID。 (CF 布爾在我的小提琴:https://jsfiddle.net/Lau1989/hxcpstty/1/

要做到這一點,我需要從這個函數訪問對象test = {}

function make_test(name, job, ID) { 
    var test = {}; //local variable to be used in 'function suggest(index)' 
    test.name = name; 
    test.job = job; 
    test.ID = ID; 
    return test; 
} 
new make_test("Paul", "manager", 1); 
new make_test("John", "employee", 2); 
new make_test("Jan", "employee", 2); 

這裏面一個:

function suggest(index) { 
    if (test.ID === index && test.name == thisInput.value) { 
     thisOutput.innerHTML = "Has job : " + test.job; //should access 'test' properties : test.name, test.job, test.ID 
    } 
} 

問題是聲明test = {};作爲全局變量將只允許function suggest(index)找到'Jan',因爲它是我聲明的最後一個。但是,如果我聲明var test = {};作爲本地變量,它將根本不工作,因爲function suggest(index)不能從外部訪問var test = {};

這是我卡住的地方。我的目標是在suggest()之內訪問var test = {};以獲取每個人的工作,具體取決於他們的nameID

感謝您的幫助

+1

您需要一個數組來保存對所有人員的引用,或者如果員工ID是唯一的(在您的示例中它不是*),則可以使用對象而不是數組。另外,調用函數時不需要'new'。所以:'var employees = []; employees.push(make_test(...));'。 – nnnnnn

+1

@Jecoms有人建議OP問另一個問題,以'建議'功能http://stackoverflow.com/questions/39968429/dynamically-access-function-object-property#comment67216922_39968451 – guest271314

+0

@nnnnnn:這就是我想要做的起初,但他們不會有我需要的其他兩個屬性(作業和ID),除非我推動數組中的對象?我最初希望避免使用數組對象,因爲迭代數組加上該對象將多次打印相同的結果。我可能是錯誤的,因爲我仍然試圖擴大我的知識在JavaScript – Lau

回答

2

鑑於你的員工ID似乎並不唯一,我建議你所有的人都存儲在一個數組,然後在suggest()功能,您可以通過數組來檢查匹配搜索(點擊運行的代碼片段嘗試一下):

function make_test(name, job, ID) { 
 
    var test = {}; 
 
    test.name = name; 
 
    test.job = job; 
 
    test.ID = ID; 
 
    return test; 
 
} 
 

 
var people = []; 
 

 
people.push(make_test("Paul", "manager", 1)); 
 
people.push(make_test("John", "employee", 2)); 
 
people.push(make_test("Jan", "employee", 2)); 
 

 
function suggest(index) { 
 
    var thisInput = document.getElementById("textarea" + index); 
 
    var thisOutput = document.getElementById("output" + index); 
 

 
    thisOutput.innerHTML = "Has job:"; 
 

 
    for (var i = 0; i < people.length; i++) { 
 
    if (people[i].ID === index && people[i].name == thisInput.value) { 
 
     thisOutput.innerHTML = "Has job: " + people[i].job; //should access 'test' properties : test.name, test.job, test.ID 
 
     break; 
 
    } 
 
    } 
 
}
<table> 
 
    <tr> 
 
    <td><textarea onkeyup="suggest(1)" name="response" id="textarea1" cols="30" rows="5"></textarea></td> 
 
    <td><div id="output1">Has job :</div></td> 
 
    </tr> 
 
    <tr> 
 
    <td><textarea onkeyup="suggest(2)" name="response" id="textarea2" cols="30" rows="5"></textarea></td> 
 
    <td><div id="output2">Has job :</div></td> 
 
    </tr> 
 
</table>

注意,它沒有意義打電話給你make_test()功能與new,因爲你手動創建函數內部的新對象。

+0

謝謝,你的解決方案是完美的!我還學到了新的技巧,這將幫助我調試更多的問題。 – Lau

+0

不客氣。有一些方法可以做得更整潔,但我認爲我會堅持一個基本的實現,只修改現有的代碼就足以讓你啓動並運行。 – nnnnnn

1

至於建議的@nnnnnn,您可以先保存make_test在返回數組對象;通過數組suggest,使用Array.prototype.forEach()遍歷數組

window.onload = function() { 
 

 
    function make_test(name, job, ID) { 
 
    var test = {}; //local variable to be used in 'function suggest(index)' 
 
    test.name = name; 
 
    test.job = job; 
 
    test.ID = ID; 
 
    return test; 
 
    } 
 

 
    var thisInput = document.querySelector("input"); 
 

 
    var thisOutput = document.querySelector("output"); 
 

 
    var arr = []; 
 

 
    arr.push(make_test("Paul", "manager", 1), 
 
    new make_test("John", "employee", 2), 
 
    new make_test("Jan", "employee", 2)); 
 

 
    function suggest(index, arr) { 
 
    arr.forEach(function(test) { 
 
     if (test.ID === index && test.name == thisInput.value) { 
 
     thisOutput.innerHTML = "Has job : " + test.job; 
 
     } 
 
    }) 
 
    } 
 

 
    suggest(1, arr); 
 

 
}
<input type="text" value="Paul"> 
 
<output></output>

相關問題