argument.html 1.09 KB
<html>
<head>
</head>
<body>
<div style="display:none">
<script>
function add1(a){
	var sum = a;
	var addMore = function addMore(b){
		sum+=b
		return addMore;
	}
	addMore.toString = function(){
		return sum
	}
	return	addMore
}

function add2 () {
    console.log('进入add');
    var args = Array.prototype.slice.call(arguments);
     console.log('args',args);
    var fn = function () {
        var arg_fn = Array.prototype.slice.call(arguments);
        console.log('调用fn');
        return add.apply(null, args.concat(arg_fn));
    }
 
    fn.valueOf = function () {
        console.log('调用valueOf');
        return args.reduce(function(a, b) {
            return a + b;
        })
    }
 
    return fn;
}

function add (...a) {
    console.log('进入add');    
    var fn = function (...b) {
        return add.apply(null, [...a,...b]);
    }
 
    fn.valueOf = function () {
        console.log('调用valueOf');
        return a.reduce(function(z, x) {
            return z + x;
        })
    }
 
    return fn;
}

console.log(add(1,2,3)(2)(3).valueOf()) // 3

</script>
</div>
</body>
</html>