javascript技巧之实现add方法无限调用
1 function add(){ 2 var slice = Array.prototype.slice, 3 args = slice.call(arguments), 4 add = add; 5 add = function(){ 6 args = args.concat(slice.call(arguments)); 7 return add; 8 }; 9 add.toString = function(){ 10 for(var result = 0, i = 0, length = args.length; i < length; i++){ 11 result += args[i]; 12 } 13 return result; 14 }; 15 return add; 16 } 17 alert(add(1)(2)(3)(4)); 18 alert(add(1,2,3,4,5)(6,7)(8)(9));
posted on 2013-10-10 13:58 shiny_bender 阅读(518) 评论(0) 编辑 收藏 举报