<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript">
//add(1)(2)(3)(4) 输出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)() 输出10
//感谢群友(大树)实现这个方法
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>
效果图: