Scope of JS functions outside Node Modules

I know that in Node, if you have variables defined outside of your module.exports, it still means locally it won't use the global namespace. This is not public. What is the publication defined in your module.

However, what about functions. Do they act the same way as variables? Can names living outside of module.exports collide?

Example:

myFirst.js

var iAmStillPrivate = "private";

module.exports = {
...whatever code I wanna expose through this module
}

function find(somethingId)
{

    ..some code
};

      

mySecond.js

var iAmStillPrivate = "private";

module.exports = {
...whatever code
}

function find(somethingId)
{

    ..some code
};

      

is find () conflicting and polluting the global namespace?

Should I do this instead ?:

mySecond.js

var iAmStillPrivate = "private";

module.exports = {
     find: find(id)
...whatever code
}

var find = function find(somethingId)
{

    ..some code
};

      

or doesn't it matter casting it into a variable? good practice? irrelevant?

Conclusion

mySecond.js (I am a fu ** ing module. I am creating an implicit anonymous function that wraps everything here when I am "required" inside other .js files)

`var iAmStillPrivate = "private";` (no I'm scoped to the module, the root anonymous function that is...)

      

(module.exports) I authorize the publication of this material. When I say publicly in Node, that is ... publicly, that stuff here is available to other modules in other .js files or whatever.)

module.exports = {
...whatever code 
}

      

(I am still executing a module function, but I have not been exported, so I am not accessible to other modules, I only belong to the module (anonymous root function)

function find(somethingId) 
{

    ..some code
};

      

+3


source to share


1 answer


The functions in each NodeJ module are local to that module and do not conflict with the functions of other modules of the same name. Think of it as if the module was wrapped in a function.

Also you don't need to define a variable for your functions, because in the end it doesn't matter, the scope of functions and variables of these two lines is the same and local to the module:

var find = function find(somethingId) ...

function find(somethingId) ...

      

Side question

Also in the comments you asked about this scenario:

what if I have a function in the global scope and the module also has a private function with the same name. do they conflict?

What happens is that inside your module, any call to that function calls the function local

, not global

. if you are outside this module or inside other modules, any call to this function will call the function global

.



Let's see this with an example. Let's assume our starting point for Node application is index.js

here:

echo('one');

require('./include.js')

echo('three');

function echo(txt){
  console.log("we are in index.js", txt);
}

      

And here is a module called include.js

:

echo('two');

function echo(txt){
  console.log("we are in include.js", txt);
}

      

if you run this application with a command node index.js

, the output should be:

we are in index.js one
we are in include.js two
we are in index.js three

      

Cm? All functions work as I explained earlier.

+4


source







All Articles