在不使用全局的情况下访问另一个函数中的局部变量
Access local variable in another function without using global
我需要将多个 textareas
的值与对象的属性相匹配。它应该只有在以下情况下才匹配:
- 正在使用的
textarea
的值等于对象中包含的人名和
- 如果textarea的索引等于人的ID。 (比照
我的 fiddle 中的布尔值: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 = {};
,根据每个人的 name
和 ID
获得他们的工作。
感谢您的帮助
正如@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>
鉴于您的员工 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>
请注意,使用 new
调用 make_test()
函数没有任何意义,因为您在函数内部手动创建了一个新对象。
我需要将多个 textareas
的值与对象的属性相匹配。它应该只有在以下情况下才匹配:
- 正在使用的
textarea
的值等于对象中包含的人名和 - 如果textarea的索引等于人的ID。 (比照 我的 fiddle 中的布尔值: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 = {};
,根据每个人的 name
和 ID
获得他们的工作。
感谢您的帮助
正如@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>
鉴于您的员工 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>
请注意,使用 new
调用 make_test()
函数没有任何意义,因为您在函数内部手动创建了一个新对象。