-
Notifications
You must be signed in to change notification settings - Fork 1
/
arguments.js.txt
97 lines (73 loc) · 2.31 KB
/
arguments.js.txt
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function sum(){
let args = Array.from(arguments);
let sum = 0;
args.forEach((ele) => {
sum += ele;
});
return sum;
}
function sum2(...args) {
let sum = 0;
args.forEach((ele) => {
sum += ele;
});
return sum;
}
// the first argument becomes the '.this' context
// the remaining arguments (the bind time args) become the arguments in the method you want to use (i.e. the bound method)
// when invoked, the arguments passed in a second parenthetical (the call time args) also get passed into the bound method
// Function.prototype.myBind = function(context) {
// let bindArgs = Array.from(arguments).slice(1);
// return (...callArgs) => {
// return this.apply(context, bindArgs.concat(callArgs));
// };
// };
Function.prototype.myBind = function(context) {
let bindArgs = Array.from(arguments).slice(1);
that = this;
return (function x () {
callArgs = Array.from(arguments);
return that.apply(context, bindArgs.concat(callArgs));
});
};
// function(context, arg1, arg2, arg3)
// ...bindArgs = [arg1, arg2, arg3]
Function.prototype.myBind2 = function(context, ...bindArgs) {
return (...callArgs) => {
return this.apply(context, bindArgs.concat(callArgs));
};
};
const sumThree = num1 => num2 => num3 => num1 + num2 + num3;
// What's the difference between?
// function curriedSum (){} --creates an object curriedSum w/ a function
// const curriedSum = function(){} --creates a variable handle for an anonymous function
const curriedSum = numArgs => {
const numbers = [];
return function _curriedSum(num) {
numbers.push(num);
if (numbers.length < numArgs) {
return numbers;
}
else{
return numbers.reduce((acc, el) => acc + el);
}
};
};
Function.prototype.curry = function (numArgs) {
const args = [];
const _curriedSum = num => {
args.push(num);
if (args.length < numArgs) {
return _curriedSum;
}
else {
return this(...args);
}
};
return _curriedSum;
};
// function sumNums(...args){
// return args.reduce((acc, el) => acc + el);
// }
//sumNums.curry(4) --method style
// const curry = Function.prototype.curry --global scope