1

I have the following code in test1.js.

module.exports = function(d){
  d.demo1 = function() {
    return "DEMO 1";
  },

  d.demo2 = function() {
    return "DEMO 2";
  }
}

I am trying to access function demo1 on test2.js. Below the code which call the function.

var demo = require('./test1');
var result = demo.****;        //code to call function demo1
console.log("calling function", result); //output should be "calling function DEMO 1"

Please help how can I access this function. Thanks.

1

2 Answers 2

7

It is very unclear what you're trying to achieve here.

You're exporting a function. That function will take 1 argument (d). Then you try to assign the demo1 and demo2 properties, of that received argument, to two different functions.

What I think you want to do is that you want to export an object with two different properties for those functions. E.g. doing this:

module.exports = {
  demo1: function() {
    return "DEMO 1";
  },

  demo2: function() {
    return "DEMO 2";
  }
}

Then you can import the module and access the demo1 and demo2 functions as:

var demo = require('./test1');
var result = demo.demo1();
Sign up to request clarification or add additional context in comments.

1 Comment

I know this, but i am trying this in different way which i ask you. Thanx i got solution from #user1280859
1
var demo = require('./test1');
var o = {};
demo(o);
o.demo1(); // "DEMO 1";

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.