访问另一个函数中的局部变量而不使用全局

时间:2016-10-11 01:17:31

标签: javascript function variables local

我需要将多个Year CAUSE Top8 2013 A000 5000 2013 A411 400 2013 A50 200 ..... 的值与对象的属性相匹配。它应该只是匹配如果:

  1. 正在使用的textareas的值等于对象
  2. 中包含的人名
  3. 如果textarea的索引等于该人的ID。 (CF 布尔在我的小提琴中:https://jsfiddle.net/Lau1989/hxcpstty/1/
  4. 为此,我需要从此函数访问对象textarea

    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 = {};找到'Jan',因为它是我声明的最后一个。但是,如果我将function suggest(index)声明为局部变量,则它将无法正常工作,因为var test = {};无法从外部访问function suggest(index)

    这就是我被困住的地方。我的目标是在var test = {};内访问var test = {};以获取每个人的工作,具体取决于他们的suggest()name

    感谢您的帮助

2 个答案:

答案 0 :(得分: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函数是没有意义的,因为您在函数内手动创建了一个新对象。

答案 1 :(得分:1)

根据@nnnnnn的建议,您可以将make_test返回的对象存储在数组中;将数组传递给suggest,使用Array.prototype.forEach()迭代数组

&#13;
&#13;
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);

}
&#13;
<input type="text" value="Paul">
<output></output>
&#13;
&#13;
&#13;