1 The logical AND and logical OR assignment operators set the left hand side 2 variable or value to the right hand side expression result depending on 3 whether the lhs value is truish. 4 5 6 1. The `&&=` operator overwrites the lhs variable or field with the rhs 7 expression result if the lhs is truish. 8 9 -- Expect stdout -- 10 [ 11 null, 12 false, 13 "is truish", 14 null, 15 false, 16 "is truish" 17 ] 18 -- End -- 19 20 -- Testcase -- 21 {% 22 x = null; 23 y = false; 24 z = true; 25 26 o = { 27 a: null, 28 b: false, 29 c: true 30 }; 31 32 res = []; 33 34 push(res, x &&= "is truish"); 35 push(res, y &&= "is truish"); 36 push(res, z &&= "is truish"); 37 38 push(res, o.a &&= "is truish"); 39 push(res, o.b &&= "is truish"); 40 push(res, o.c &&= "is truish"); 41 42 printf("%.J\n", res); 43 %} 44 -- End -- 45 46 47 2. The `||=` operator overwrites the lhs variable or field with the rhs 48 expression result if the lhs is falsy. 49 50 -- Expect stdout -- 51 [ 52 "is falsy", 53 "is falsy", 54 true, 55 "is falsy", 56 "is falsy", 57 true 58 ] 59 -- End -- 60 61 -- Testcase -- 62 {% 63 x = null; 64 y = false; 65 z = true; 66 67 o = { 68 a: null, 69 b: false, 70 c: true 71 }; 72 73 res = []; 74 75 push(res, x ||= "is falsy"); 76 push(res, y ||= "is falsy"); 77 push(res, z ||= "is falsy"); 78 79 push(res, o.a ||= "is falsy"); 80 push(res, o.b ||= "is falsy"); 81 push(res, o.c ||= "is falsy"); 82 83 printf("%.J\n", res); 84 %} 85 -- End -- 86 87 88 3. Ensure that the assignment value expression is not evaluated if the 89 assignment condition is false. 90 91 -- Expect stdout -- 92 [ 93 false, 94 false, 95 true, 96 false, 97 false, 98 true, 99 0, 100 0, 101 0, 102 0, 103 0, 104 0 105 ] 106 -- End -- 107 108 -- Testcase -- 109 {% 110 a = 0; 111 b = 0; 112 c = 0; 113 d = 0; 114 e = 0; 115 f = 0; 116 117 o = { 118 a: false, 119 b: false, 120 c: true 121 }; 122 123 x = false; 124 y = false; 125 z = true; 126 127 res = []; 128 129 push(res, x ??= a++); 130 push(res, y &&= b++); 131 push(res, z ||= c++); 132 133 push(res, o.a ??= d++); 134 push(res, o.b &&= e++); 135 push(res, o.c ||= f++); 136 137 printf("%.J\n", [ ...res, a, b, c, d, e, f ]); 138 %} 139 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt