argument.html
1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<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>