0

I want to call function A to function B.

I already tried to call function A from function B

module.exports = function(req, res) {
  return {
    function_a: function() {
      // do something here
    },
    function_b: function() {
      // call function_a
      const A = function_a(); // I want to be called something like this
      // do something here
    }
  }
}

I expect function A to be called inside function B

5
  • please add how do you like to call the functions. Commented Oct 16, 2019 at 7:50
  • Its pretty simple stuff, in order for you to be able to call any function it must be visible (available to the scope of the caller). Commented Oct 16, 2019 at 7:51
  • 3
    Your existing code has syntax errors (you seem to be mixing up object literal and function body syntax). You need to deal with that before worrying about how to access one function from the other (because how you fix that problem seriously influences how you solve the problem you are asking about) Commented Oct 16, 2019 at 7:52
  • Here you are telling your exports is going to be function module.exports = function(req, res) { then when you do this function_a: function() { // do something here }, Commented Oct 16, 2019 at 7:56
  • already edited the function. can you help? Commented Oct 16, 2019 at 7:58

6 Answers 6

2

Assuming that function_b is called via:

your_module().function_b();

Then you can access the object with this inside function_b.

this.function_a();

If the method gets detached from the object, then you'll lose that connection so it might be worth rewriting the module so you have a reference that doesn't depend on the value of this. For example:

module.exports = function(req, res) {

  const my_return_value = {
    function_a: function() {
      // do something here
    },
    function_b: function() {
      const A = my_return_value.function_a();
    }
  };

  return my_return_value;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please include an example for If the method gets detached from the object, then you'll lose that connection
wow this working just perfect. I will try to get the logic from this. thanks.
jQuery.ajax("some url").then( your_module().function_b ) would detach the method from the object (for example).
0

The code you currently have is not valid JavaScript. You're trying to handle functions as an object inside a function (??).

Here's what you can do: first create the functions outside of module.exports

function funcA(){
  console.log('Inside function A!');
}

function funcB(){
  funcA();
  console.log('inside function B!');
}

This will behave like any two functions would. Calling funcA() will log 'Inside function A!' and calling funcB() will log 'Inside function A!' 'inside function B!' as expected.

You can then export them to be used elsewhere:

exports = {
  funcA: funcA,
  funcB: funcB
}

Here we are using exports rather than module.exports which is equivalent. The above can be simplified with ES6: {funcA, funcB} since key and value have the same name.

Comments

0

Try this. you can also visit this link to see the different styles of handling your problem. https://gist.github.com/kimmobrunfeldt/10848413

function a() {

}

function b() {
    a()
}

module.exports = {
    a: a,
    b: b
}

Comments

0

To refer another function inside an object, you would use this keyword.

This snippet might be helpful to you.

function name (firstName, lastName) {
  return {
    function_a: function() {
	return firstName
    },
    function_b: function() {
      const A = this.function_a(); 
	return A + lastName 
    }
  }
}

const initName = name('rohit', 'bhatia') 

console.log(initName.function_b())

Adding on, Quentin answer might be more helpful/useful

Comments

0

Your code is not valid in javascript
It is supposed to be

module.exports = function(req, res) {
  const function_a = function() {
    // do something here
  }
  const function_b = function() {
    // call function_a
    function_a()
    // do something here
  }
}

Or you may mean export a Object not a Function

module.exports = {
  function_a:function() {
    // do something here
  },
  function_b:function() {
    // call function_a
    this.function_a()
    // do something here
  }
}

Comments

-2

This looks very confusing but I assume you want something like this:


module.exports = function(req, res) {
   functionA() {};

   functionB(){
     functionA();
   }
}

If this is not the answer you're looking for please tell us what do you want to export from the module.

1 Comment

I already get the logic but I don't know how to do it. and your code above can't be used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.