简体   繁体   English

在不使用全局的情况下访问另一个函数中的局部变量

[英]Access local variable in another function without using global

I need to match the value of multiple textareas to the properties of an object. 我需要将多个textareas的值与对象的属性进行匹配。 It should only be a match if : 仅在以下情况下才是匹配项:

  1. the value of the textarea being used is equal to the person's name contained in the object and 正在使用的textarea的值等于对象中包含的人名, 并且
  2. if the index of the textarea is equal to the person's ID. 如果textarea的索引等于该人的ID。 (Cf boolean in my fiddle : https://jsfiddle.net/Lau1989/hxcpstty/1/ ) (我的小提琴中的boo布尔值: https : //jsfiddle.net/Lau1989/hxcpstty/1/

To do this I need to access the object test = {} from this function : 为此,我需要从此函数访问对象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);

Inside this one : 在这个里面:

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
    }
}

Problem is that declaring test = {}; 问题是声明test = {}; as a global variable will only allow function suggest(index) to find 'Jan' because it is the last one I declared. 作为全局变量,将只允许function suggest(index)查找“ Jan”,因为它是我声明的最后一个。 But if I declare var test = {}; 但是如果我声明var test = {}; as a local variable it won't work at all because function suggest(index) can't access var test = {}; 作为局部变量,它根本不起作用,因为function suggest(index)无法访问var test = {}; from outside. 从外面。

This is where I'm stuck. 这就是我卡住的地方。 My goal is to access var test = {}; 我的目标是访问var test = {}; within suggest() to get every person's job depending on their name and ID . suggest()根据每个人的nameID获得每个人的工作。

Thanks for your help 谢谢你的帮助

Given that your employee IDs don't seem to be unique, I'd suggest you store all the people in an array, then in your suggest() function you can search through the array to check for a match (click Run code snippet to try it out): 鉴于您的员工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> 

Note that it doesn't make sense to call your make_test() function with new , because you manually create a new object inside the function. 注意,用new调用make_test()函数没有任何意义,因为您是在函数内部手动创建一个新对象。

As suggested by @nnnnnn, you can store object returned by make_test in an array; 如@nnnnnn所建议,您可以将make_test返回的对象存储在数组中; pass array to suggest , use Array.prototype.forEach() to iterate array 通过数组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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM