Java Reference
In-Depth Information
else {
// Simply return the value
return value;
}
}
function reviver (key, value) {
if (key === "x" || key === "y") {
// Divide the value by 2
return value / 2;
}
else {
return value;
}
}
var pointString = JSON.stringify(point, replacer, " ");
print("Stringified object is");
print(pointString);
var obj = JSON.parse(pointString, reviver);
print("Parsed object proeprties are");
for(var prop in obj) {
print("obj[" + prop + "] = " + obj[prop]);
}
Stringified object is
{
"x": 20,
"y": 40
}
Parsed object proeprties are
obj[x] = 10
obj[y] = 20
Dynamically Evaluating Scripts
Nashorn contains a global function called eval(string) . It takes a string containing
Nashorn code as an argument, executes the code, and returns the last evaluated value in
the code. The following snippet of code shows a trivial example of adding two numbers
using the eval() function:
// Assigns 300 to sum
var sum = eval("100 + 200");
 
Search WWH ::




Custom Search