<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <script type="text/javascript"> //add(1)(2)(3)(4) output 10 function add(x){ var sum = x; var tmp = function(y){ sum = sum + y; return tmp; }; tmp.toString = function(){ return sum; }; return tmp; } var result = add(1)(2)(3)(4); alert(result); //add(1)(2)(3)(4)() output 10 //Thanks to group friends (big tree) for implementing this method function add2(x) { return function(y) { if (typeof y !== 'undefined') { x = x + y; return arguments.callee; } else { return x; } }; } var result2 = add2(1)(2)(3)(4)(); alert(result2); </script> </body> </html>
Effect picture: