gpt4 book ai didi

javascript - 如何一次定义很多变量

转载 作者:行者123 更新时间:2023-11-28 16:59:33 26 4
gpt4 key购买 nike

我正在开发一个项目,想知道是否有一种简单的方法可以定义和使用多个变量,而无需编写一堆代码。

所以我一直在做的只是像这样定义它们:

var Kahoot = require("kahoot.js-updated");
var client1 = new Kahoot;
var client2 = new Kahoot;
var client3 = new Kahoot;
var name = "example";
var id = "12345";


然后像这样使用它们:

client1.join(id , name+"1").then(() => {client1.on("question", question => {});client1.on("questionStart", question => {question.answer(randomAnswer());})});
client2.join(id , name+"2").then(() => {client2.on("question", question => {});client2.on("questionStart", question => {question.answer(randomAnswer());})});
client3.join(id , name+"3").then(() => {client3.on("question", question => {});client3.on("questionStart", question => {question.answer(randomAnswer());})});

这很好用,但我必须手动复制和粘贴,然后替换变量号。有什么更简单的方法可以做到这一点而无需一遍又一遍地复制粘贴?

最佳答案

只需创建一个 array .

var Kahoot = require('kahoot.js-updated');
var name = 'example';
var id = '12345';

var clients = new Array(3);
for (var i = 0; i < 3; i++) {
var client = new Kahoot();
client.join(id, name + (i + 1)).then(() => {
client.on('question', question => {});
client.on('questionStart', question => {
question.answer(randomAnswer());
});
});
clients[i] = client;
}

然后数组 clients 包含您的三个客户端。

关于javascript - 如何一次定义很多变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57960802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com