<p>The caveat when using NetscriptJS to write scripts is that your code must be
asynchronous. Furthermore, instead of using the global scope and executing your code
sequentially, NetscriptJS uses a <codeclass="code docutils literal"><spanclass="pre">main()</span></code> function as an entry point.</p>
<p>Furthermore, the "Netscript environment" must be passed into a NetscriptJS script through
the main function. This environment includes all of the pre-defined Netscript functions
(<codeclass="code docutils literal"><spanclass="pre">hack()</span></code>, <codeclass="code docutils literal"><spanclass="pre">exec</span></code>, etc.) as well as the arguments you pass to the script.</p>
<p>Therefore, the signature of the <codeclass="code docutils literal"><spanclass="pre">main()</span></code> function must be:</p>
<p>Here is a summary of all rules you need to follow when writing Netscript JS code:</p>
<ul>
<li><pclass="first">Write <codeclass="code docutils literal"><spanclass="pre">await</span></code> before any call to the following Netscript functions:</p>
<blockquote>
<div><ulclass="simple">
<li>hack</li>
<li>grow</li>
<li>weaken</li>
<li>sleep</li>
<li>run</li>
<li>exec</li>
<li>prompt</li>
</ul>
</div></blockquote>
</li>
<li><pclass="first">Any function that contains <codeclass="code docutils literal"><spanclass="pre">await</span></code> must be declared as <codeclass="code docutils literal"><spanclass="pre">async</span></code></p>
</li>
<li><pclass="first">Always <codeclass="code docutils literal"><spanclass="pre">await</span></code> any function that is marked as <codeclass="code docutils literal"><spanclass="pre">async</span></code></p>
</li>
<li><pclass="first">Any functions that you want to be visible from other scripts must be marked with <codeclass="code docutils literal"><spanclass="pre">export</span></code>.</p>
</li>
<li><pclass="first"><strong>Do not write any infinite loops</strong> without using a <codeclass="code docutils literal"><spanclass="pre">sleep</span></code> or one of the timed Netscript functions like <codeclass="code docutils literal"><spanclass="pre">hack</span></code>. Doing so will crash your game.</p>
</li>
<li><pclass="first">Any global variable declared in a NetscriptJS script is shared between all instances of that
script. For example, assume you write a script <em>foo.ns</em> and declared a global variable like so:</p>
<p>Then, you ran multiple instances of <em>foo.ns</em>:</p>
<divclass="highlight-default"><divclass="highlight"><pre><span></span>$ run foo.ns 1
$ run foo.ns 1 2 3
$ run foo.ns 1 2 3 4 5
</pre></div>
</div>
<p>Then all three instances of foo.ns will share the same instance of <codeclass="code docutils literal"><spanclass="pre">globalVariable</span></code>.
(In this example, the value of <codeclass="code docutils literal"><spanclass="pre">globalVariable</span></code> will be set to 5 because the
last instance of <em>foo.ns</em> to run has 5 arguments. This means that all three instances of
the script will repeatedly print the value 5).</p>
<p>These global variables can be thought of as <aclass="reference external"href="https://www.tutorialspoint.com/cplusplus/cpp_static_members.htm">C++ static class members</a>,
where a NetscriptJS script is a class and a global variable is a static member within that class.</p>
</li>
</ul>
</div>
<divclass="section"id="warnings">
<h2>Warnings<aclass="headerlink"href="#warnings"title="Permalink to this headline">¶</a></h2>
<p>The NetscriptJS evaluation engine works by converting your code into a blob URL and then
using a dynamic import to load your code as a module. Every unique NetscriptJS script
is loaded as its own module. This means that
making a small edit to a NetscriptJS script results in a new module being generated.</p>
<p>At this point, we have been unable to find a method for deleting modules from browsers so that
they get garbage collected.</p>
<p>The result is that these modules from NetscriptJS scripts accumulate in your browser,
using memory that never gets released. Over time, this results in a memory-leak type
situation that can slow down your computer.</p>
<p>Therefore, there are two recommendations for those who decide to use NetscriptJS:</p>
<p>1. Every now and then, close and re-open the game. This will clear all of the modules.
To be safe, I recommend <strong>completely</strong> closing the game's tab and then re-opening it.
Depending on your browser, a refresh or page reload does not always clear the modules.</p>
<p>2. Only use NetscriptJS scripts when needed. It is very unlikely that NetscriptJS
is needed for very simple scripts. By doing this, you will reduce the number of modules
that are loaded.</p>
</div>
<divclass="section"id="examples">
<h2>Examples<aclass="headerlink"href="#examples"title="Permalink to this headline">¶</a></h2>
<p>This script shows some of the new functionality that is available in NetscriptJS,
including objects and object constructors, changing an object's prototype, and
importing other NetscriptJS scripts:</p>
<divclass="highlight-default"><divclass="highlight"><pre><span></span>import {tprintColored} from "tprintColored.ns"; //Importing from other NetscriptJS scripts works!
function ScriptJob(params) {
if (params.fn == null) {
throw new Error("No Filename (fn) passed into ScriptJob ctor");