Skip to content

Latest commit

 

History

History
16 lines (12 loc) · 825 Bytes

global.loadWithNewGlobal.md

File metadata and controls

16 lines (12 loc) · 825 Bytes

loadWithNewGlobal

loadWithNewGlobal is similar to the load function in that it loads a script from a file, a URL, or a script object. However, unlike load, loadWithNewGlobal creates a new ECMAScript global scope object and loads the script into it. The loaded script's global definitions are placed in this new global scope. Furthermore, modifications made by the loaded script to built-in objects (such as String.prototype.indexOf) are not reflected in the caller's global scope; these modifications only affect the newly created global scope.

Sample

loadWithNewGlobal({
    script: "foo = 333; print(foo)",
    name: "test"
});
 
// prints undefined as "foo" is defined in the new global and not here
print(typeof foo);

from https://wiki.openjdk.org/display/Nashorn/Nashorn+extensions