[英]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 : 仅在以下情况下才是匹配项:
textarea
being used is equal to the person's name contained in the object and textarea
的值等于对象中包含的人名, 并且 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()
根据每个人的name
和ID
获得每个人的工作。
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.