Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not exactly sure what you're asking here, but if you need to pass values into your modules you should make sure to export functions that accept parameters. When they say that a module is cached, it means that your module is only initialized once. Think of your module as an object:</p> <pre><code>var a = 1; var b = 2; console.log("init!"); module.exports.test = function(){ console.log("this is a function!"); } </code></pre> <p>Here, a, b, and the first log will run only once. This is when the module is requested and is then cached. When you do a </p> <pre><code> var example = require("module") </code></pre> <p>If it was never created it'll initialize a, b, and do the log message. If it was already created it will just give you a reference to what you exported. Each time you call:</p> <pre><code> example.test() </code></pre> <p>It will output: <code>this is a function!</code></p> <p>But you will NOT get the a, b, and the first log run again.</p> <p>Think of all statements not exported as private static variables of that object. </p> <p>Here is another fully working example:</p> <p><code>app.js</code></p> <pre><code>var s = require("./sample"); var y = require("./sample"); s.test(); y.test(); s.test(); console.log("finished"); </code></pre> <p><code>sample.js</code></p> <pre><code>var a = 1; var b = 2; console.log("init!"); function test() { console.log("here! " + a); a++; } exports.test = test; </code></pre> <p>This all outputs:</p> <pre><code>init! here! 1 here! 2 here! 3 finished </code></pre> <p>Does this help at all?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload