• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/00_syntax/19_arrow_functions

  1 Besides the ordinary ES5-like function declarations, ucode supports ES6 inspired
  2 arrow function syntax as well. Such arrow functions are useful for callbacks to functions such as replace(), map() or filter().
  3 
  4 -- Expect stdout --
  5 () => { ... }
  6 test
  7 (a, b) => { ... }
  8 3
  9 (...args) => { ... }
 10 15
 11 (a) => { ... }
 12 10
 13 (a) => { ... }
 14 36
 15 -- End --
 16 
 17 -- Testcase --
 18 {%
 19 
 20         // assign arrow function to variable
 21         test1_fn = () => {
 22                 return "test";
 23         };
 24 
 25         // assign arrow function with parameters
 26         test2_fn = (a, b) => {
 27                 return a + b;
 28         };
 29 
 30         // nesting functions is legal
 31         test3_fn = (...args) => {
 32                 nested_fn = (a, b) => {
 33                         return a * b;
 34                 };
 35 
 36                 return args[0] + nested_fn(args[0], args[1]);
 37         };
 38 
 39         // parentheses may be omitted if arrow function takes only one argument
 40         test4_fn = a => {
 41                 return a * 2;
 42         };
 43 
 44         // curly braces may be omitted if function body is a single expression
 45         test5_fn = a => a * a;
 46 
 47         print(join("\n", [
 48                 test1_fn,
 49                 test1_fn(),
 50                 test2_fn,
 51                 test2_fn(1, 2),
 52                 test3_fn,
 53                 test3_fn(3, 4),
 54                 test4_fn,
 55                 test4_fn(5),
 56                 test5_fn,
 57                 test5_fn(6)
 58         ]), "\n");
 59 %}
 60 -- End --
 61 
 62 
 63 While the main advantage of arrow functions is the compact syntax, another
 64 important difference to normal functions is the "this" context behaviour -
 65 arrow functions do not have an own "this" context and simply inherit it from
 66 the outer calling scope.
 67 
 68 -- Expect stdout --
 69 this is set to obj: true
 70 arrow function uses outher this: true
 71 normal function has own this: true
 72 arrow function as method has no this: true
 73 -- End --
 74 
 75 -- Testcase --
 76 {%
 77         obj = {
 78                 method: function() {
 79                         let that = this;
 80                         let arr = () => {
 81                                 print("arrow function uses outher this: ", that == this, "\n");
 82                         };
 83                         let fn = function() {
 84                                 print("normal function has own this: ", that != this, "\n");
 85                         };
 86 
 87                         print("this is set to obj: ", this == obj, "\n");
 88 
 89                         arr();
 90                         fn();
 91                 },
 92 
 93                 arrowfn: () => {
 94                         print("arrow function as method has no this: ", this == null, "\n");
 95                 }
 96         };
 97 
 98         obj.method();
 99         obj.arrowfn();
100 %}
101 -- End --
102 
103 
104 Due to the difficulty of recognizing arrow function expressions with an LR(1)
105 grammar the parser has to use a generic expression rule on the lhs argument list
106 and verify that it does not contain non-label nodes while building the ast. The
107 subsequent testcase asserts that case.
108 
109 -- Expect stderr --
110 Syntax error: Unexpected token
111 Expecting ';'
112 In line 2, byte 10:
113 
114  `    (a + 1) => { print("test\n") }`
115   Near here --^
116 
117 
118 -- End --
119 
120 -- Testcase --
121 {%
122         (a + 1) => { print("test\n") }
123 %}
124 -- End --
125 
126 
127 Arrow functions consisting of a single expression implicitly return the expression
128 results. Arrow functions having a statement block as body do not return any result
129 by default but may return explictly.
130 
131 -- Expect stdout --
132 [
133         4,
134         null,
135         4
136 ]
137 -- End --
138 
139 -- Testcase --
140 {%
141         printf("%.J\n", [
142                 (() => 2 * 2)(),
143                 (() => { 2 * 2 })(),
144                 (() => { return 2 * 2 })()
145         ]);
146 %}
147 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt