Convert Library to JavaScript closure <p>I have an external library of JavaScript functions that I need to pull into my server-side Google Apps Script project. I'm doing this mainly for performance reasons, but also because <a href="https://developers.google.com/apps-script/guide_libraries" rel="nofollow">Google now recommends against using external libs</a>.</p> <p>In the current configuration, functions in the external library are referenced by <code>&lt;Libname&gt;.funcName()</code>, and any globals in the library are likewise contained, e.g. <code>&lt;Libname&gt;.globA</code>. If I just copy the library into a file in my project, those functions (and "globals") would be referenced as <code>funcName()</code> alone. I've got hundreds of such references in my project that potentially need to change. That's fine; it's something a global search &amp; replace can address. However, I have worries about "namespace" problems... Since the library is third-party code, I've had no control over it, and now I'm running into name collisions, as objects and functions in the library have the same names as some of my code.</p> <p>How can I pull this code into my project in a way that will keep its internal details separate from my code? Is there a way to do that and retain the current naming convention, <code>&lt;Libname&gt;.funcName()</code>? I believe that JavaScript closures are the right solution, but don't know enough about them.</p> <h3>My project, <code>Code.gs</code></h3> <pre><code>var globA = { key: 'aKey', value: 'valA' }; function myFunction() { var a = MyLib.getValue(); Logger.log( JSON.stringify( a ) ); // { key: 'libKey', value: 'valLibA' }; } function getValue() { return globA; } </code></pre> <h3>Library "MyLib", <code>Code.gs</code></h3> <pre><code>var globA = { key: 'libKey', value: 'valLibA' }; function getValue( ) { return globA; } </code></pre>