2

if you have a simple javascript file foo.js like so:

function doSomething (){
console.log('done');
}

module.exports = doSomething;

if this file/module is required, it will get cached (any information about this online that you could point me to would be great).

so, in another JS file we have:

var foo1 = require('foo'); // yup

and in yet another file we have:

var foo2 = require('foo'); // points to same cache as foo1

but what happens with the cache when we call:

var foo3 = new foo2();  // doesn't point to same cache as foo2 and foo1?

I assume a new copy of something is created, but what exactly happens beneath the surface? I am wondering this because I want to know how robust a particular singleton pattern is in JavaScript.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

0 Answers0