Itzik984
Itzik984

Reputation: 16774

Exporting inner function from object in node.js

If i have a certain object say:

function object1(){

  function func(){...}

 *rest of objec1 content*  

 ...}

 exports.func=func;  //<---this is wrong (compiler error);

is there a way for me to export func() from outside object1 bounds?

or,within the object1 bounds, and in this case, how to use it from another place?

Upvotes: 1

Views: 1049

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074535

You've omitted part of your source, but what you have looks fine if I fill in the blanks:

function object1(){

  function func(){...}

  exports.func=func; 
}

Something needs to call object1 at some point, e.g.:

object1();

or

new object1();

...in order for the exports.func = func; line to run, but that's fine provided you do it.

Or did you mean this doesn't work:

function object1(){

  function func(){...}

}

exports.func=func; 

If so, of course not, there's no func symbol defined in the scope where you're using it. You'd have to do something like the first code block above.

Upvotes: 4

Related Questions