JS add(1)(2)(3)(4)() output 10

<!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:

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326892557&siteId=291194637