Java Reference
In-Depth Information
// Use the phoneFormatter() function as the replacement in the replace()
// method
var formattedPhones = text.replace(pattern, phoneFormatter);
print("Phones: " + text);
print("Formatted Phones: " + formattedPhones);
Phones: 3342449999, 2229822, and 6152534734
Formatted Phones: (334) 244-9999, 222-9822, and (615) 253-4734
Knowing Script Location
Nashorn provides three global objects:
__FILE__
__DIR__
__LINE__
Notice the two underscores before and after the names of these properties. They
contain the script file name, script file's directory name, and the line number of the script
that reads the __LINE__ property. The properties __FILE__ , and __DIR__ may be reported
as null if they are not available such as when reading scripts from standard input.
Sometimes, you will get a file name that is not the real file name of your script. You may
run your script in a file from a Java program using a Reader , and in that case, the file name
will be reported as <eval> . Listing 4-28 contains code that prints these properties. The
code is stored in a file named scriptdetails.js .
Listing 4-28. The Contents of the scriptdetails.js File
// scriptdetails.js
// Print the location details of the following statement
print("File Name =", __FILE__, ", Directory =",__DIR__, ", Line # =" ,__LINE__);
The following command runs the scriptdetails.js file on the command-line using
the jjs command that prints the script details:
c:\>jjs c:\kishori\scriptdetails.js
File Name = c:\kishori\scriptdetails.js, Directory = c:\kishori\, Line # = 4
 
Search WWH ::




Custom Search