0

New to Node.js, and Im wondering if it's possible to pass a parameter to a module function? Something similar to this:

module.js:

module.exports = {
  example1: function(parameter) {
    return "hello, world";
  },

  example2: function(parameter) {
    return "hello,world";
  }
};

Then call it like so...

var mod = require("./module.js")
mod.example1(passedParameter);

Thanks!

2
  • 2
    Yes it is. But you might have been quicker just trying that out than posting a question and waiting for someone to answer. ;) Commented May 19, 2016 at 4:39
  • @BadIdeaException For some reason when I tried what I listed above it crashed :/ Wasn't trying to be lazy! haha Commented May 19, 2016 at 11:34

1 Answer 1

2

A function attached as an exported property as shown in your answer is just a function like any other function. You decide what arguments you want it to accept and process and what arguments to pass it. It's just like any other Javascript function definition - you decide how to define and use it. The fact that it's an exported property from a module does not make any difference. It's just a Javascript function like any other. It happens to live in a particular module, but other than that, you can do anything with it that you can with any other function definition.

I'm wondering if it's possible to pass a parameter to a module function?

Yes, it is possible and is common.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.