简体   繁体   中英

how to call a function inside variable?

This probably is a basic question but i am stuck here.

const activate = (server, plugin) => {
  let handlers = {
    async beginBlock(request) {
      //...my functon code here
    }
  }
};

I want to know how to call beginBlock() function from a outside library? so far I tried activate.handlers.beginBlock(request) , which didn't work.

Try this

const activate = (server, plugin) => {
    const beginBlock = async (request) => {
        //...my functon code here
    }
    return beginBlock;
};

const beginBlock = activate(server, plugin);
beginBlock(request);

I'd like to expand on Rahul Sharma's response with slightly updated code that will allow you to have more than one function available for calling from outside your library.

const activate = (server, plugin) => {
    const beginBlock = async (request) => {
        //...my functon code here
    }
    const endBlock = async (request) => {
        //... my function code here
    }
    return {
        beginBlock: beginBlock,
        endBlock: endBlock
    };
};

const beginBlock = activate(server, plugin);
beginBlock(request);

const endBlock = activate(server, plugin);
endBlock(request);

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