Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Nashorn Syntax Extensions

Function expression closures

...

Code Block
titleback quote exec strings
// exec "ls -l" to get file listing as a string
var files = `ls -l`
var lines = files.split("\n");

// print only the directories
for (var l in lines) {
    var line = lines[l];
    if (line.startsWith("d")) // directory
        print(line)
}

Nashorn script API extensions

__proto__ property

Like other ECMAScript implementations (like Rhino, v8) Nashorn supports mutable __proto__ magic property to read and write prototype of a given object. See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto

Object.setPrototypeOf(obj, newProto)

ECMAScript specifies Object.getPrototypeOf(obj) function to get prototype of the given object. This nashorn specific extension allows prototype of an object to be set by newProto.

Object.bindProperties

Object.bindProperties lets you bind properties of one object to another object. Example:

Code Block
titleObject.bindProperties
var obj = {}

var obj2 = { foo: 344 }

// bind properties of 'obj2' to 'obj'
Object.bindProperties(obj, obj2);

print(obj.foo);    // obj2.foo
obj.foo = "hello"  // obj2.foo assigned
print(obj2.foo);   // prints "hello"

// bind to global 'this' is also possible
Object.bindProperties(this, obj2);

print(foo);        // prints obj2.foo
foo = 42;          // assigns to obj2.foo
print(obj2.foo);   // prints 42


The bound object need not be a script object. It could be a Java object as well - usual Java bean style getter/setter properties will be bound. Also a property only with getter will be bound as read-only property.

Extensions of Error objects, Error.prototype and Error constructor

Nashorn extends ECMAScript Error (or subtype) objects with the following properties.

  • lineNumber - source code line number from which error object was thrown
  • columnNumber  -  source code column number from which error object was thrown
  • fileName  - source script file name
  • stack - script stack trace as a string

In addition to this Error.prototype has the following extension functions as well.

  • Error.prototype.printStackTrace() - prints full stack trace (including all Java frames) from where error was thrown from
  • Error.prototype.getStackTrace() -  returns an array of java.lang.StackTraceElement instance for ECMAScript frames only.

Error constructor includes dumpStack() function property

  • Error.dumpStack() - prints stack trace of the current thread - just like java.lang.Thread.dumpStack()

 

Code Block
titleError extensions
function func() {
    throw new Error()
}

function f() {
    func()
}

try {
    f()
} catch (e) {
    print(e.stack)
    print(e.lineNumber)
    print(e.columnNumber)
    print(e.fileName)
}

String.prototype extensions

String.prototype.trimLeft - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/TrimLeft

String.prototype.trimRight - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/TrimRight