Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --harmony-arrow-functions | |
| 6 | |
| 7 // Arrow functions are like functions, except they throw when using the | |
| 8 // "new" operator on them. | |
| 9 assertEquals(typeof (() => {}), "function"); | |
|
rossberg
2014/07/14 13:55:46
Nit: expected value should go left (similarly belo
| |
| 10 assertEquals(Object.getPrototypeOf(() => {}), Function.prototype); | |
| 11 assertThrows("new (() => {})", TypeError); | |
| 12 | |
| 13 // Check the different syntax variations | |
| 14 assertEquals(1, (() => 1)()); | |
| 15 assertEquals(2, (a => a + 1)(1)); | |
| 16 assertEquals(3, (() => { return 3; })()); | |
| 17 assertEquals(4, (a => { return a + 3; })(1)); | |
| 18 assertEquals(5, ((a, b) => a + b)(1, 4)); | |
| 19 assertEquals(6, ((a, b) => { return a + b; })(1, 5)); | |
| 20 | |
| 21 // The following are tests from: | |
| 22 // http://wiki.ecmascript.org/doku.php?id=harmony:arrow_function_syntax | |
| 23 | |
| 24 // Empty arrow function returns undefined | |
| 25 var empty = () => {}; | |
| 26 assertEquals(empty(), undefined); | |
| 27 | |
| 28 // Single parameter case needs no parentheses around parameter list | |
| 29 var identity = x => x; | |
| 30 assertEquals(identity(empty), empty); | |
| 31 | |
| 32 // No need for parentheses even for lower-precedence expression body | |
| 33 var square = x => x * x; | |
| 34 assertEquals(square(3), 9); | |
| 35 | |
| 36 // Parenthesize the body to return an object literal expression | |
| 37 var key_maker = val => ({key: val}); | |
| 38 assertEquals(key_maker(empty).key, empty); | |
| 39 | |
| 40 // Statement body needs braces, must use 'return' explicitly if not void | |
| 41 var evens = [0, 2, 4, 6, 8]; | |
| 42 var odds = evens.map(v => v + 1); | |
| 43 assertEquals(odds, [1, 3, 5, 7, 9]); | |
| 44 | |
| 45 var fives = []; | |
| 46 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].forEach(v => { if (v % 5 === 0) fives.push(v); } ); | |
|
rossberg
2014/07/14 13:55:46
Nit: line length
| |
| 47 assertEquals(fives, [5, 10]); | |
| OLD | NEW |