Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
Logical OR operator is a shortcut operator,
and when using ||
and LHS is false
, the RHS will be evaluated.
var b = a || 10; // initializing 10 when a is false.
This can be also used with assignment operator as ||=
.
a ||= new A(); // creating an instance only when a is false.
function test(a) {
return a || 10;
}
System.println(test(0));
System.println(test(1));
System.println(test(null));
10
1
10
var id;
class A() { @id = id++; }
function test(a) {
a ||= new A();
return a;
}
var a;
id = 10;
System.println((a = test(a)).id);
System.println((a = test(a)).id);
System.println((a = test(a)).id);
10
10
10