Include JS file within private scope
<p>I simply want the ability to include a JS file, but not have it evaluated in the global scope.</p>

<p>I have skimmed over labjs and requirejs, and although they can do 1000 other things it doesn't seem that either is able to solve this problem.</p>

<p>I am aware that I could wrap the code of <code>foo.js</code> such that it expects a certain context, and can act within in, but that is not what I'm looking for (having to alter the source files). Rather, I would like the source JS files to remain as any other JS file that doesn't need any sort of metadata or any resolving of the runtime execution context via code <em>in the included file itself</em>; outside of the included file, that is ok.</p>

<p>Some simple demonstrations for clarity:</p>

<pre><code>/*
 * Example 1 - apply to current context
 */
function x() {  
    include('foo.js'); // provides foo()
    foo(); // ok!
}
foo(); // not ok!


/*
 * Example 2 - apply to namespace context
 */
include.apply(ns, ['foo.js']); // provides foo()
ns.foo(); // ok!
foo(); // not ok!
</code></pre>

<p>I know that this can likely be achieved using <code>eval()</code> or by creating a <code>new Function</code> with the string, but I am hoping that there is a better solution.</p>
