rest-with-arguments.js (812B)
1 // 'arguments' is allowed with rest parameters. 2 3 var args; 4 5 function restWithArgs(a, b, ...rest) { 6 return arguments; 7 } 8 9 args = restWithArgs(1, 3, 6, 9); 10 assertEq(args.length, 4); 11 assertEq(JSON.stringify(args), '{"0":1,"1":3,"2":6,"3":9}'); 12 13 args = restWithArgs(); 14 assertEq(args.length, 0); 15 16 args = restWithArgs(4, 5); 17 assertEq(args.length, 2); 18 assertEq(JSON.stringify(args), '{"0":4,"1":5}'); 19 20 function restWithArgsEval(a, b, ...rest) { 21 return eval("arguments"); 22 } 23 24 args = restWithArgsEval(1, 3, 6, 9); 25 assertEq(args.length, 4); 26 assertEq(JSON.stringify(args), '{"0":1,"1":3,"2":6,"3":9}'); 27 28 function g(...rest) { 29 h(); 30 } 31 function h() { 32 g.arguments; 33 } 34 g(); 35 36 // eval() is evil, but you can still use it with rest parameters! 37 function still_use_eval(...rest) { 38 eval("x = 4"); 39 } 40 still_use_eval();