function sum(a) {
result = 0;
for (i=0; i <
a.length; i++)
result += a[i];
return result;
}
as
function sum(a) {
var result = 0;
for (var i=0; i
< a.length; i++)
result += a[i];
return result;
}
This is not equivalent code because the second version does
not modify global variables result and i. However, if you don't intend for any other function to
access these variables, then storing them globally is probably wrong anyway
(what if you called another function that had a loop like the one in sum!).
var a = new Array();
for (var i=0; i < n; i++)
a[i] = i;
could be sped up by changing the constructor call to new Array(n). A constructor call like that indicates to
the runtime that a Java array should be used for the first n entries of the array. Similarly,
new
Array("a", "b", "c") or ["a", "b", "c"] will cause a 3-element
Java array to be allocated to hold the contents of the JavaScript array.
function MyObject(a) {
this.s = a;
this.toString = new
Function("return this.s");
}
could be written more efficiently as
function MyObject(a) {
this.s = a;
this.toString =
function () { return this.s }
}
Beginning with Rhino 1.4 Release 2, code
passed to eval and new Function will be interpreted rather than compiled to
class files.