-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
204 lines (186 loc) · 5.04 KB
/
code.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
var version = ["1.34.5", "1.23123.10", "1.1.1"];
function cmp(a, b) {
var len_a = a.length;
var len_b = b.length;
var i = 0;
var j = 0;
for (; i < len_a, j < len_b; i++, j++) {
var str_a = '';
while (a[i] != '.' && i < len_a) {
str_a += a[i];
i++;
}
var num_a = Number(str_a);
var str_b = '';
while (b[j] != '.' && j < len_b) {
str_b += b[j];
j++;
}
var num_b = Number(str_b);
if (num_b === num_a) {
continue;
} else {
return num_b < num_a;
}
}
}
version.sort(cmp);
console.log(version);
var s = ['a', 'f', 'g', 'k'];
var cmp1 = function () {
var number = Math.random();
return number > 0.5;
}
s.sort(cmp1);
console.log(s);
function debounce(fn, wait) {
let timer = null;
return function () {
var context = this;
var args = arguments;
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(function () {
fn.apply(context, args);
}, wait)
}
}
function throttle(fn, wait) {
let timer = null;
return function () {
var context = this;
var args = arguments;
if (timer) {
return;
}
timer = setTimeout(function () {
fn.apply(context, args);
timer = null;
}, wait);
}
}
let myInstanceof = (target, origin) => {
while (target) {
if (target.__proto__ === origin.prototype) {
return true;
}
target = target.__proto__;
}
return false;
}
Array.prototype.myMap = function (fn, thisValue) {
let res = [];
thisValue = thisValue || [];
let arr = this;
for (let i in arr) {
res.push(fn(arr[i]));
}
return res;
}
function depyCpoy(newObj, oldObj) {
for (var k in oldObj) {
let item = oldObj[k];
if (item instanceof Array) {
newObj[k] = [];
depyCpoy(newObj[k], item);
}
if (item instanceof Object) {
newObj[k] = {};
depyCpoy(newObj[k], item);
} else {
newObj[k] = item;
}
}
}
var foo = function (a, b) {
// return Object.prototype.toString.call(arguments);
// return [...arguments];// return Array.from(arguments);return Array.prototype.slice.call(arguments);
}
console.log(foo(1, 2, 3)) //[object Arguments]
function _new(fun) {
return function () {
let obj = {
__proto__: fun.prototype
}
fun.apply(obj, arguments);
return obj;
}
}
function person(name, age) {
this.name = name
this.age = age
}
let obj = _new(person)('LL', 100)
console.log(obj) //{name: 'LL', age: 100}
function _new1(fun) {
return function () {
let obj = {
__proto__: fn.prototype
}
fun.apply(obj, arguments);
return obj;
}
}
JSON.stringify();
JSON.parse();
function ajax() {
let xhr = new XMLHttpRequest();
xhr.open('get', "url");
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
let string = xhr.respondeText;
let object = JSON.parse(string);
}
}
}
xhr.send(null);
}
const EventUtils = {
// 视能力分别使用dom0||dom2||IE方式 来绑定事件
// 添加事件
addEvent: function (element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent("on" + type, handler);
} else {
element["on" + type] = handler;
}
},
// 移除事件
removeEvent: function (element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else if (element.detachEvent) {
element.detachEvent("on" + type, handler);
} else {
element["on" + type] = null;
}
},
// 获取事件目标
getTarget: function (event) {
return event.target || event.srcElement;
},
// 获取 event 对象的引用,取到事件的所有信息,确保随时能使用 event
getEvent: function (event) {
return event || window.event;
},
// 阻止事件(主要是事件冒泡,因为 IE 不支持事件捕获)
stopPropagation: function (event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
},
preventDefault: function (event) { // 取消事件的默认行为
if (event.preventDefault) {
event.preventDefault();
} else {
event.returnValue = false;
}
}
};