- Loading...
...
...
| Code Block | ||
|---|---|---|
| ||
// 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)
}
|
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
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 lets you bind properties of one object to another object. Example:
| Code Block | ||
|---|---|---|
| ||
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.
Nashorn extends ECMAScript Error (or subtype) objects with the following properties.
In addition to this Error.prototype has the following extension functions as well.
Error constructor includes dumpStack() function property
| Code Block | ||
|---|---|---|
| ||
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.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