Java Reference
In-Depth Information
■
the
loadWithNewGlobal()
function is useful in executing script in a new thread
using a new global scope, thus eliminating the possibility of the thread overwriting the
existing global variables. Both functions return a value that is the value of the last evaluated
expression in the loaded script.
Tip
You have used the
load()
function to load the scripts in this chapter. You can specify
the local path (absolute or relative) or a URL of a script:
// Load a script from a local path from the current directory
load("Point.js");
// Load a script from a local path using an absolute path on Windows
load("C:\\scripts\\Point.js");
The
load()
and
loadWithNewGlobal()
functions also support pseudo URL schemes
such as n
ashorn
and
fx
. The
nashorn
scheme is used to load Nashorn scripts; for
example,
load("nashorn:mozilla_compat.js")
loads the built-in Mozilla compatibility
script so that you can use Mozilla functionalities in Nashorn. The
fx
scheme is used to
load JavaFX related scripts; for example,
load("fx:controls.js")
loads the built-in
JavaFX script that imports all JavaFX control class names into the Nashorn code, so you
can use their simple names.
The
load()
and
loadWithNewGlobal()
method can also load and evaluate a script
in an object called a script object. A script object should contain two properties named
name
and
script
. The
name
property specifies a name for the script that is used as the
script name when an error is thrown. You can give any name for your script you want.
The
script
property contains the script to be loaded and evaluated. Both properties are
mandatory. The following code creates a script object that prints the current date on the
standard output, loads, and evaluates the script using the
load()
function. You may get a
different output as the script prints the current date and time:
// Create a script object
var scriptObject = {
name: "myscript",
script: "var today = new Date; print('Today is', today);"
};
// Load and evaluate the script object
load(scriptObject);
Today is Wed Oct 08 2014 11:56:50 GMT-0500 (CDT)
Search WWH ::
Custom Search