JavaScript 如何在函数中创建新对象?
JavaScript How to create a new object in a function?
我正在编写一个 discord 机器人,它将通过对象存储来自消息的请求。
您的想法是您调用一个函数,该函数将创建一个新对象,该对象可以作为一种存储信息的方式被引用,而不是每次请求显示所述信息时都必须引用一个巨大的文件或变量.
目前我的代码是用我想要的基本版本设置的。
var order1 = {
content: "",
author: "",
}
var order2 = {
content: "",
author: "",
}
var order3 = {
content: "",
author: "",
}
即使从我有限的编程经验来看,我也知道这是重复的事情,而且通常有更有效的方法来编写它。
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
// Interpret Command
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
var messagecont = message.content.replace('!haul.order', ""); // Remove command string
if(command === 'haul.order'){
message.channel.send("Hual order for:" + messagecont + " by: " + message.author.username);
orderNum++; // Update the current number order
if (orderNum > 3) {
message.channel.send("Sorry we only have 3 storage objects! Our programmer is to lazy to fix
this!");
}
if (orderNum == 1) {
order1.content = messagecont;
order1.author = message.author.username + ". ";
} else if (orderNum == 2) {
order2.content = messagecont;
order2.author = message.author.username + ". ";
} else if (orderNum == 3) {
order3.content = messagecont;
order3.author = message.author.username + ". ";
}
} else if (command =="show.orders") {
message.channel.send("Orderlist:" + order1.content + " by: " + order1.author + order2.content + " by: " + order2.author + order3.content + " by: " + order3.author);
}
});
为了演示,此代码目前只有三个存储对象,但是添加更多会“解决”问题,但方式不对。我再问一次,有没有办法通过函数创建一个新对象?诸如 order1 之类的东西被创建,而不是 order2。我知道 Create.object() 存在,但据我所知,它仅将模板应用于您必须声明的变量。
通过将 订单 存储在 array. To such array you may push()
中,可以根据需要添加任意多的条目。
//REM: Containing all the orders
const _listOfOrders = [];
document.querySelector('button').addEventListener('click', function(){
//REM: Getting input values
let tContent = document.getElementById('content')?.value || '';
let tAuthor = document.getElementById('author')?.value || '';
//REM: Adding the values to the list of orders
_listOfOrders.push({
Content: tContent,
Author: tAuthor
});
//REM: Outputting the current list
console.table(_listOfOrders)
});
<input type = 'text' id = 'content' value = 'Content'>
<input type = 'text' id = 'author' value = 'Author'>
<button>order</button>
打开控制台查看结果。
我正在编写一个 discord 机器人,它将通过对象存储来自消息的请求。
您的想法是您调用一个函数,该函数将创建一个新对象,该对象可以作为一种存储信息的方式被引用,而不是每次请求显示所述信息时都必须引用一个巨大的文件或变量.
目前我的代码是用我想要的基本版本设置的。
var order1 = {
content: "",
author: "",
}
var order2 = {
content: "",
author: "",
}
var order3 = {
content: "",
author: "",
}
即使从我有限的编程经验来看,我也知道这是重复的事情,而且通常有更有效的方法来编写它。
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
// Interpret Command
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
var messagecont = message.content.replace('!haul.order', ""); // Remove command string
if(command === 'haul.order'){
message.channel.send("Hual order for:" + messagecont + " by: " + message.author.username);
orderNum++; // Update the current number order
if (orderNum > 3) {
message.channel.send("Sorry we only have 3 storage objects! Our programmer is to lazy to fix
this!");
}
if (orderNum == 1) {
order1.content = messagecont;
order1.author = message.author.username + ". ";
} else if (orderNum == 2) {
order2.content = messagecont;
order2.author = message.author.username + ". ";
} else if (orderNum == 3) {
order3.content = messagecont;
order3.author = message.author.username + ". ";
}
} else if (command =="show.orders") {
message.channel.send("Orderlist:" + order1.content + " by: " + order1.author + order2.content + " by: " + order2.author + order3.content + " by: " + order3.author);
}
});
为了演示,此代码目前只有三个存储对象,但是添加更多会“解决”问题,但方式不对。我再问一次,有没有办法通过函数创建一个新对象?诸如 order1 之类的东西被创建,而不是 order2。我知道 Create.object() 存在,但据我所知,它仅将模板应用于您必须声明的变量。
通过将 订单 存储在 array. To such array you may push()
中,可以根据需要添加任意多的条目。
//REM: Containing all the orders
const _listOfOrders = [];
document.querySelector('button').addEventListener('click', function(){
//REM: Getting input values
let tContent = document.getElementById('content')?.value || '';
let tAuthor = document.getElementById('author')?.value || '';
//REM: Adding the values to the list of orders
_listOfOrders.push({
Content: tContent,
Author: tAuthor
});
//REM: Outputting the current list
console.table(_listOfOrders)
});
<input type = 'text' id = 'content' value = 'Content'>
<input type = 'text' id = 'author' value = 'Author'>
<button>order</button>
打开控制台查看结果。