Search code examples
node.jsexpresskoa

why they don't use Object when exporting data or event?


I am trying to understand node and react through books and some example like Todos.

but I've never seen they use Object when they wanted to export data.

Why don't they use it?

As long as I use Object, I don't need to add the data or event when I exports them.

example

const insertUser = async (userData) => {
 ***
};

const loginAction = async (where) =>{
    ***
}

const checkDuplicationID = async (userId) => {
    ****
}
//you should add the event when you export event whenever events are added. 
module.exports = { loginAction, insertUser, checkDuplicationID }

my opinion

let userActions = {}

userActions.insertUser = async (userData) => {
    ****
};

userActions.loginAction = async (where) =>{
   ****
}

userActions.checkDuplicationID = async (userId) => {
   ****
}

//you don't need to add the event when you export event.
module.exports = { userActions }

Is there any problems if I use Object?


Solution

  • there is no problem using objects, in javascript, almost everything is an object. you can export the methods like this

    module.exports = {
      insertUser: async (userData) => {
        // logic
      },
      loginAction: async (where) => {
        // logic
      },
      checkDuplicationID: async (userId) => {
        // logic
      }
    }
    

    you can import/require the module and use it in other modules

    // import or require
    const myMethods = require('./path/filename');
    
    // call the method
    myMethods.insertUser();
    myMethods.loginAction();
    myMethods.checkDuplicationID();