-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit.js
More file actions
60 lines (53 loc) · 1.16 KB
/
init.js
File metadata and controls
60 lines (53 loc) · 1.16 KB
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
/**
* 初始化模式:即时函数、对象即时初始化、条件初始化
*/
/*
* 即时函数
*/
(function() {
// 所有 module 的代码……
}());
/*
* 对象即时初始化
*/
({
maxwidth: 600,
maxheight: 400,
gimmeMax: function() {
return this.maxwidth + "x" + this.maxheight;
},
init: function() {
console.log(this.gimmeMax());
}
}).init();
/*
* 条件初始化
*/
var win = window,
doc = document,
utils = {
addListener: null,
removeListener: null
};
if (typeof win.addEventListener === 'function') {
utils.addListener = function(el, type, fn) {
el.addEventListener(type, fn, false);
};
utils.removeListener = function(el, type, fn) {
el.removeEventListener(type, fn, false);
};
} else if (typeof doc.attachEvent === 'function') { // IE
utils.addListener = function(el, type, fn) {
el.attachEvent('on' + type, fn);
};
utils.removeListener = function(el, type, fn) {
el.detachEvent('on' + type, fn);
};
} else { // older browsers
utils.addListener = function(el, type, fn) {
el['on' + type] = fn;
};
utils.removeListener = function(el, type, fn) {
el['on' + type] = null;
};
}