1 Ucode borrows the `"use strict";` statement from ECMA script to enable 2 strict variable semantics for the entire script or for the enclosing 3 function. 4 5 With strict mode enabled, attempts to use undeclared local variables 6 or attempts to read global variables which have not been assigned yet 7 will raise an exception. 8 9 10 1. To enable strict mode for the entire script, it should be the first 11 statement of the program. 12 13 -- Expect stderr -- 14 Reference error: access to undeclared variable x 15 In line 4, byte 8: 16 17 ` print(x);` 18 ^-- Near here 19 20 21 -- End -- 22 23 -- Testcase -- 24 {% 25 "use strict"; 26 27 print(x); 28 %} 29 -- End -- 30 31 32 2. To enable strict mode for a single function, the "use strict" expression 33 should be the first statement of the function body. 34 35 -- Expect stdout -- 36 a() = null 37 -- End -- 38 39 -- Expect stderr -- 40 Reference error: access to undeclared variable x 41 In b(), line 9, byte 24: 42 called from anonymous function ([stdin]:13:4) 43 44 ` printf("b() = %J\n", x);` 45 Near here -------------------^ 46 47 48 -- End -- 49 50 -- Testcase -- 51 {% 52 function a() { 53 printf("a() = %J\n", x); 54 } 55 56 function b() { 57 "use strict"; 58 59 printf("b() = %J\n", x); 60 } 61 62 a(); 63 b(); 64 %} 65 -- End -- 66 67 68 3. When "use strict" is not the first statement, it has no effect. 69 70 -- Expect stdout -- 71 b=null 72 c=null 73 -- End -- 74 75 -- Testcase -- 76 {% 77 function t() { 78 a = 1; 79 80 "use strict"; 81 82 printf("b=%J\n", b); 83 } 84 85 t(); 86 87 "use strict"; 88 89 printf("c=%J\n", c); 90 91 %} 92 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt