简体   繁体   中英

JavaScript How to create a new object in a function?

I'm writing up a discord bot that will store a request from a message via an object.

The idea is you call a function that will create a new object that can be referenced as a way to store information, rather than having 1 giant file or variable that has to be referenced every time a request to display said information.

Currently my code is setup with a rudimentary version of what I want.

var order1 = {
    content: "",
    author: "",
}
var order2 = {
    content: "",
    author: "",
}
var order3 = {
    content: "",
    author: "",
} 

Even from my limited experience of programming, I know that is something is repeated, and often, there is usually a more effective way to write it.

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);
    }
});

For demonstration this code currently has only three storage objects, however adding more would "fix" the issue but in the wrong way. I ask again, is there a way to create a new object via a function? Something like order1, than order2 gets created. I know Create.object() exists, but from my knowledge, it only applies a template to a variable you had to declare.

It would be more dynamic by storing the orders in an array . To such array you may push() as many entries as you like.

 //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>

Open the console to see the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM