-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingleton.js
57 lines (51 loc) · 1.39 KB
/
singleton.js
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
function makeInvoker(func) {
var result;
if (result) {
return result;
}
const operators = Array.prototype.slice.call(arguments, 1, arguments.length)
result = func.apply(this, operators);
return result
}
function generator(...operators) {
function invoker(...args) {
let _this;
operators.forEach(operator => {
_this = operator.apply(this, args)
});
return _this;
}
return invoker
}
// 函数重载 todo
const insert = function (target, base) {
target.count = base || 0;
return target;
}
// 只运行一次
const increase = function (target, base, increaseNum) {
target.count += (increaseNum || 1)
return target;
}
const decrease = function (target, base, increaseNum, decreaseNum) {
const _decreaseNum = decreaseNum || 1;
target.count -= _decreaseNum
return target;
}
function test() {
let obj = {};
let obj2 = {};
let base = 1;
let increaseNum = 3;
let decreaseNum = 2;
let invoker = makeInvoker(generator, insert, increase, decrease)
let invoker2 = makeInvoker(generator, insert, decrease, increase, increase)
let invoker3 = makeInvoker(generator, decrease)
let result = invoker(obj, base, 0, decreaseNum);
console.log(result)
let result2 = invoker2(obj2, base, increaseNum, decreaseNum);
let result3 = invoker3(obj, undefined, 3)
console.log(result2, result2 === result)
console.log(result3, result3 === result)
}
// test();