search

Home  >  Q&A  >  body text

javascript - Dynamically generate n js instance objects


I want to control the number of elevators through buttons. What I first thought was to create an instance object through a for loop when the button is clicked, but the object variable name is changed after the for loop. becomes undefined, how should I create an instance object and call it in other codes

It should be that n elevators are connected in parallel. Later, I need to control which elevator to pick up people, so what I want is to generate 4 elevators through a for loop. Each elevator has a variable name, and then use the variable name to call the prototype method of the elevator.

漂亮男人漂亮男人2753 days ago688

reply all(1)I'll reply

  • 学习ing

    学习ing2017-06-10 09:50:24

    let obj = {};
    for (let i = 0; i < 4; i++) {
        obj[i] = {};
    }
    console.log(obj);

    reply
    0
  • Cancelreply