-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (40 loc) · 898 Bytes
/
index.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
//index.js
var _ = {
//once
once: function(argFun){
var isFirst = true;
var result;
if(typeof argFun !== 'function'){
throw new TypeError('Expected a function');
}
return function(){
if(isFirst){
isFirst = false;
result = argFun.apply(this, arguments);
}
return result;
};
},
memoize: function(argFun, resolver){
if(typeof argFun !== 'function'|| (resolver && typeof resolver != 'function')){
throw new TypeError('Expected a function');
}
var cache = {};
var memoized = function(){
var key = resolver ? resolver.apply(this, arguments):arguments[0];
if(cache.hasOwnProperty(key)){
return cache[key];
}
var value = argFun.apply(this, arguments);
cache[key] = value;
return value;
};
return memoized;
},
bind: function(fn, context){
return function(){
return fn.apply(context, arguments);
};
}
};
module.exports = _;