Exporting inner function from object in node.js

Problem

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?

Problem courtesy of: Itzik984

Solution

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.

Solution courtesy of: T.J. Crowder

View additional discussion.