0

我正在运行的实验有一个问题,我需要创建 40 个班级成员(研究具有某些属性的老鼠,例如动物 ID、笼子 ID、颜色偏好、光线偏好),然后对它们进行一些操作。所有这些属性都是通过用户输入获得的(例如鼠标 37B、笼子 37、黑色、明亮)。一个好的解决方案可能是创建一个数组并为每个后续元素迭代一个 for 循环并将其定义为类成员。我在下面尝试了这个,但它没有用,所以我不知道它是否可能。简而言之,实际问题是如何通过不为它们显式编写名称来创建类的多个自定义命名成员?

        var mice = [];

        class Mouse
        {
          constructor(id, cage, color, light) 
          {
            this.id = id;
            this.cage = cage;
            this.color = color;
            this.light = light;
          }
        }

        for (var i = 0; i < 40; i++) 
        {
           mice[i] = new Mouse(user_input1, user_input2, user_input3, user_input4);
        }
4

2 回答 2

0

在尝试创建一个最小的可重现示例时,我的代码开始工作。尝试通过附加 js 库(实际上在我测试它时工作)从 excel 文件中提供输入时,原始文件相当复杂,所以有些事情可能出错了。我发现了一些不正确的声明,现在似乎一切正常;我能够将数组元素分配为类成员,这就是我所追求的;现在循环这个应该很容易。谢谢大家的帮助,很抱歉第一次发帖很草率;我对此很陌生。

这是我的最小可复制示例。

  <!DOCTYPE html>
  <html>
  <head>
    <title>CPP randomizer</title>
  </head>
  <body>
    Animal ID: <input id="ID"><br>
    Cage number: <input type="number" id="cage"><br>
    Color: <input id="color"><br>
    Light preference: <input id="light"><br>
    <button onclick="myFunction()">Submit</button>

 
    <script type="text/javascript">

    var mice = [];
            
    class Mouse
    {
      constructor(id, cage, color, light) 
      {
        this.id = id;
        this.cage = cage;
        this.color = color;
        this.light = light;
      }
    }

    function myFunction()
      {
        var id = document.getElementById("ID").value;
        var cage = document.getElementById("cage").value;
        var color = document.getElementById("color").value;
        var light = document.getElementById("light").value;
        mice[0] = new Mouse(id, cage, color, light);
      alert(mice[0].id);
      alert(mice[0].cage);
      alert(mice[0].color);
      alert(mice[0].light);
      }

  </script>
  
</body>
于 2020-07-30T23:25:28.343 回答
0

我通过将 constarctor 上的输入替换为循环中的值来编辑您的代码,然后将输入添加到 Enter mouse id 并对其进行编辑

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<input id="mouse_id" type="text" placeholder="mouse_number_id" value=""></input>
<button onclick="edit_lab_mouse()">edit</bytton>
<script>

  var mice = [];

        class Mouse
        {
          constructor(id, cage, color, light) 
          {
            this.id = id;
            this.cage = cage;
            this.color = color;
            this.light = light;
          }
        }

        for (var i = 0; i < 40; i++) 
        {
           mice[i] = new Mouse(i, i, i, i);
        }
     
          
        function edit_lab_mouse(){
        var mouse_id=document.getElementById("mouse_id").value;
         mice[mouse_id].color = prompt("Please enter mouse color");
        mice[mouse_id].cage = prompt("Please enter mouse cage");
          mice[mouse_id].light= prompt("Please enter light value");
         mice[mouse_id].id = prompt("Please enter new mouse id");
         
       alert("updated value done");
        }
       

</script>
</body>
</html>

于 2020-07-30T22:01:45.503 回答