sk22
sk22

Reputation: 867

JavaScript / Node.js: function returning another function based on parameters

Here's what I'm trying to achieve:

When the Node.js application loads, many different things are set up, and e.g. requires.has('message', 'text') is called. This should set up a function that checks if the passed data object contains message.text.

The following code registers an handler (The syntax is bot.register(callback, requirements)).

bot.register(irrelevant, [requires.has('message', 'text')]);

When a http request comes in, all requirements are executed: So, a callback must be generated from requires.has('message', 'text').

So, in short:

requires.has('message', 'text')

must return

{
  callable: function(data) { // check if data has nodes message and text }
}

Sorry if that explanation sounds confusing, I hope you know what I mean. Thanks in advance!

Upvotes: 0

Views: 342

Answers (1)

Iso
Iso

Reputation: 3238

So what you want to do is a function that returns another function as an object key.

requires.has = function(parameters) {
  return {
    callable: function (data) {
      // your implementation, has access to parameters & data variables
    }
  };
};

Upvotes: 1

Related Questions