Unbinds a variable from other closures using it.
Prexonite Script, like JavaScript, does not have scoped control-structures.
That means that closures that refer to loop variables will in most cases not work as expected.
function main(){
var n = 15;
function f1()
{
while(n > 4)
println(n--);
}
f1();
println(n); // "4"
n = 13;
new var v;
f1();
println(n); // "13"
}
After the new var n
statement, Prexonite considers n
to be a fresh variable that has no relationship with any of the scopes that
n
was shared with previously (f1
in this case).
The variable n
does, however, retain the value of its predecessor.
function main(){
var xs = [1,2,3,4,5,6,7];
var fs = [];
foreach(var x in xs){
fs[] = y => x + y;
}
println(x); // "7"
println(fs[3].(5)); // "12" == 7+5
fs = [];
foreach(new var x in xs){
fs[] = y => x + y;
}
println(x); // "7"
println(fs[3].(5)); // "9" == 4+5
}
While it is possible to use y => new var x + y
to achieve a similar effect, it is in most
cases not recommended since that causes the variable to be unbound every single time the closure is invoked.
Note that new var v
is different from var new v
.
The latter is used to avoid capturing v
in the first place.