-
Notifications
You must be signed in to change notification settings - Fork 7
/
useful.js
134 lines (134 loc) · 3.62 KB
/
useful.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Created with JetBrains WebStorm.
* User: Tianyi(99)
* Date: 13-3-29
* Time: 下午12:05
* To change this template use File | Settings | File Templates.
*/
;
(function () {
var toolkit = {
throttle:function (fn, delay) {
var timer = null;
return function () {
var context = this, args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
fn.apply(context, args);
}, delay);
};
},
uncurryThis:function (f) {
return function () {
return f.call.apply(f, arguments)
};
},
curryThis:function (f) {
return function () {
var a = Array.prototype.slice.call(arguments);
a.unshift(this);
return f.apply(null, a);
};
},
before : function (f) {
var _self = this;
return function () {
if (f.apply(this, arguments) === false) {
return false;
}
return _self.apply(this, arguments)
};
},
before : function (f) {
var _self = this;
return function () {
var result = return _self.apply(this, arguments)
if ( === false) {
return false;
}
f.apply(this, arguments);
return result;
};
},
connect:function (scope, fnFrom, fnTo) {
"use strict"
var objFn = fnFrom.split('.');
var deepth = objFn.length;
var scope = scope || window;
var j = deepth, i = j;
var _obj = scope, __obj = _obj;
while (i > 0) {
_obj = _obj[objFn[deepth - i]];
i -= 1;
}
var t = function () {
return function () {
var ret = _obj.apply(this, arguments);
fnTo.apply(this, arguments);
return ret;
}
}
while (j > 1) {
__obj = __obj[objFn[deepth - j]];
j -= 1;
}
__obj[objFn[deepth - 1]] = t();
},
Timer = {
_data : {},
start : function (key) {
Timer._data[key] = new Date();
}
stop : function (key) {
var time = Timer._data[key];
if (time) {
Timer._data[key] = new Date - time;
}
}
getTime : function (key) {
return Timer._data[key];
}
},
simpleAtQuene:function (toDoList, args, callback) {
var todo = toDoList.concat();
setTimeout(function () {
var task = todo.shift();
task.apply(null, args || []);
//console.log('todo = '+todo.length)
if (todo.length > 0) {
setTimeout(arguments.callee, 25)
} else {
callback()
}
}, 25)
},
memorize:function (fun){
var buffer = {};
var cache = function(){
var key = Array.prototype.join.call(arguments, '');
if(!(key in buffer)){ buffer[key] = fun.apply(this, arguments); };
return buffer[key];
}
cache.del = function(key){
if(!key){
buffer = {};
}else if(key in buffer){
delete buffer[key];
}
}
cache.add = function(key, value){
buffer[key] = value;
}
return cache;
},
timeTest = function(funName) {
var that = this;
return function() {
var d = +new Date();
var apy = that.apply(this,arguments);
console.log(funName+" need time:"+(+new Date() - d));
return apy;
}
};
return tookit
})()