-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
298 lines (267 loc) · 7.84 KB
/
utils.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* 这是一些常用的代码片段,以后写demo时候可以直接复制或是参考
*/
// 深拷贝一个对象(简单实现)
function cloneObject(src) {
var result;
if (Object.prototype.toString.call(src) == '[object Array]') {
result = [];
} else {
result = {};
}
for (var key in src) {
if (src.hasOwnProperty(key)) {
if (typeof src[key] === 'object') {
result[key] = arguments.callee(src[key]);
} else {
result[key] = src[key];
}
}
}
return result;
}
// 类似于Object.assign,实现对象的合并,拷贝,参考jQuery.extend
function assign() {
var target = arguments[0],
len = arguments.length,
deep = false,
options,
src,
clone,
copy;
// 最后一个参数传入true则表示合并时采取深拷贝
if (typeof arguments[len - 1] === 'boolean') {
deep = arguments[len - 1]
len--
}
for (var i = 1; i < len; i++) {
if ((options = arguments[i]) != null) {
for (var name in options) {
src = target[name]
copy = options[name]
if (target === copy) continue
if (deep && copy) {
target[name] = cloneObject(copy)
} else if (copy !== undefined) {
target[name] = copy
}
}
}
}
return target
}
// 获取element相对于浏览器窗口的位置,返回一个对象{x, y}
function getPosition(element) {
var actualLeft = element.offsetLeft,
currentLeft = element.offsetParent,
actualTop = element.offsetTop,
currentTop = element.offsetParent;
while (currentLeft !== null) {
actualLeft += currentLeft.offsetLeft;
currentLeft = currentLeft.offsetParent;
}
while (currentTop !== null) {
actualTop += currentTop.offsetTop;
currentTop = currentTop.offsetParent;
}
return {
x: actualLeft,
y: actualTop
}
}
// 发布订阅模式
function PubSub() {
this.handers = {};
}
PubSub.prototype = {
on: function (eventType, handler) {
if (typeof this.handers[eventType] == 'undefined') {
this.handers[eventType] = [];
}
this.handers[eventType].push(handler);
},
emit: function (eventType) {
let handlerArgs = Array.prototype.slice.call(arguments, 1)
for (let i = 0; i < this.handers[eventType].length; i++) {
this.handers[eventType][i].apply(this, handlerArgs);
}
}
}
/**
* ajax简单封装,返回一个promise对象
* options = {
* url:'',
* headers:{}
* type:'POST',
* data:{}
* }
* @param options
*/
function ajax(options) {
return new Promise(function (resolve, reject) {
let xhr = new XMLHttpRequest(),
url = options.url || '',
type = url.type || 'GET',
headers = options.headers
xhr.open(type, url)
for (let i in options.headers) {
xhr.setRequestHeader(i, headers[i]);
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
resolve(xhr.responseText)
} else {
reject(xhr.status)
}
}
}
xhr.send(options.data || null)
})
}
// js的流程控制,用队列实现一个lazyman
(function (window) {
var queue = []
function next() {
var fn = queue.shift()
fn && fn()
}
function _LazyMan(name) {
this.name = name
var fn = (function (name) {
return function () {
console.log('Hi This is'+name)
next()
}
})(name)
queue.push(fn)
setTimeout(function () {
next()
},0)
}
_LazyMan.prototype = {
sleep:function (s) {
var fn = (function (time) {
return function () {
setTimeout(function () {
next()
},time)
}
})(s)
queue.push(fn)
return this
},
eat:function (food) {
var fn = (function (food) {
return function () {
console.log('Eat'+food)
next()
}
})(food)
queue.push(fn)
return this
},
sleepFirst:function (s) {
var fn = (function (time) {
return function () {
setTimeout(function () {
next()
},time)
}
})(s)
queue.unshift(fn)
return this
}
}
function LazyMan(name) {
return new _LazyMan(name)
}
window.LazyMan = LazyMan
})(window)
// 简单的Promise实现
;(function (window){
function Promise(executor){
var self = this
self.status = 'pending'
self.data = undefined
self.onResolvedCallback = []
self.onRejectedCallback = []
function resolve(value){
if (self.status === 'pending') {
self.status = 'resolved'
for(var i = 0; i < self.onResolvedCallback.length; i++){
self.onResolvedCallback[i](value)
}
}
}
function reject(reason){
if (self.status === 'pending') {
self.status = 'rejected'
for(var i = 0; i < self.onRejectedCallback.length; i++){
self.onRejectedCallback[i](reason)
}
}
}
try{
executor(resolve,reject)
}catch(e){
reject(e)
}
}
Promise.prototype.then = function(onResolved, onRejected) {
var self = this
var promise2
onResolved = typeof onResolved === 'function' ? onResolved : function(v) {}
onRejected = typeof onRejected === 'function' ? onRejected : function(r) {}
if (self.status === 'resolved') {
return promise2 = new Promise(function(resolve, reject) {
try {
var x = onResolved(self.data)
if (x instanceof Promise) {
x.then(resolve, reject)
}
resolve(x)
} catch (e) {
reject(e)
}
})
}
if (self.status === 'rejected') {
return promise2 = new Promise(function(resolve, reject) {
try {
var x = onRejected(self.data)
if (x instanceof Promise) {
x.then(resolve, reject)
}
} catch (e) {
reject(e)
}
})
}
if (self.status === 'pending') {
return promise2 = new Promise(function(resolve, reject) {
self.onResolvedCallback.push(function(value) {
try {
var x = onResolved(value)
if (x instanceof Promise) {
x.then(resolve, reject)
}
} catch (e) {
reject(e)
}
})
self.onRejectedCallback.push(function(reason) {
try {
var x = onRejected(self.data)
if (x instanceof Promise) {
x.then(resolve, reject)
}
} catch (e) {
reject(e)
}
})
})
}
}
window.MyPromise = Promise
})(window)