-
Notifications
You must be signed in to change notification settings - Fork 4
/
3.手写集合三.js
356 lines (330 loc) · 9.15 KB
/
3.手写集合三.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// 1. 倒计时偏差解决
// 1. 倒计时偏差解决
;
(function (done) {
if (!done) return
// 1 请求服务端校验
// 2 setTimeout 计算偏差值 每次校验
const interval = 1000;
// 从服务器和活动开始时间计算出的时间差,这里测试用 50000 ms
let ms = 50000;
let count = 0;
const startTime = new Date().getTime();
let timeCounter;
if (ms >= 0) {
timerCounter = setTimeout(countDownStart, interval);
}
function countDownStart() {
count++;
const offset = new Date().getTime() - (startTime + count * interval);
let nextTime = interval - offset;
if (nextTime < 0) {
nextTime = 0;
}
ms -= interval;
console.log(
`误差:${offset} ms,下一次执行:${nextTime} ms 后,离活动开始还有:${ms} ms`
);
if (ms < 0) {
clearTimeout(timeCounter);
} else {
timeCounter = setTimeout(countDownStart, nextTime);
}
}
})()
// 2 setTimeout模拟setIntervalr
;
(function (done) {
if (!done) return
function mySetInterval(fn, timeout) {
function interval() {
fn();
setTimeout(interval, timeout);
}
setTimeout(interval, timeout);
}
//
function mySetInterval2(fn, timeout) {
// 控制器,控制定时器是否继续执行
var timer = {
flag: true,
};
// 设置递归函数,模拟定时器执行。
function interval() {
if (timer.flag) {
fn();
setTimeout(interval, timeout);
}
}
// 启动定时器
setTimeout(interval, timeout);
// 返回控制器
return timer;
}
})()
// 3 模拟实现symbol 1
;
(function (done) {
if (!done) return
var root = this;
var SymbolPolyfill = function Symbol(description) {
// 实现特性第 2 点:Symbol 函数前不能使用 new 命令
if (this instanceof SymbolPolyfill)
throw new TypeError("Symbol is not a constructor");
// 实现特性第5点,如果 Symbol 的参数是一个对象,就会调用该对象的 toString 方法,将其转为字符串,然后才生成一个 Symbol 值。
var description =
description === undefined ? undefined : String(description);
var symbol = Object.create(null);
Object.defineProperties(symbol, {
__Description__: {
value: description,
writable: false,
enumerable: false,
configurable: false,
},
});
// // 实现特性第 6 点,因为调用该方法,返回的是一个新对象,两个对象之间,只要引用不同,就不会相同
root.SymbolPolyfill = SymbolPolyfill;
};
})()
// 3.2 模拟实现symbol 2
;
(function (done) {
if (!done) return
// 第2版
var root = this;
var generateName = (function () {
var postfix = 0;
return function (descString) {
postfix++;
return "@@" + descString + "_" + postfix;
};
})();
var SymbolPolyfill = function Symbol(description) {
if (this instanceof SymbolPolyfill)
throw new TypeError("Symbol is not a constructor");
var descString =
description === undefined ? undefined : String(description);
var symbol = Object.create({
toString: function () {
return this.__Name__;
},
});
Object.defineProperties(symbol, {
__Description__: {
value: descString,
writable: false,
enumerable: false,
configurable: false,
},
__Name__: {
value: generateName(descString),
writable: false,
enumerable: false,
configurable: false,
},
});
return symbol;
};
root.SymbolPolyfill = SymbolPolyfill;
})()
// 3.3 参考
;
(function (done) {
if (!done) return
const GlobalSymbolRegistry = {};
const INTERNAL = {
hasInstance: true,
isConcatSpreadable: true,
iterator: true,
match: true,
replace: true,
search: true,
species: true,
split: true,
toPrimitive: true,
toStringTag: true,
unscopables: true,
};
const generateName = (function () {
const created = {};
return function (description, internal) {
let postfix;
if (INTERNAL[description]) {
postfix = "";
INTERNAL[description] = false;
} else {
postfix = created[description] =
created[description] === undefined ? 1 : created[description] + 1;
}
return `@@${description || ""}${postfix}`;
};
})();
function d(mode, value) {
const map = {
c: "configurable",
e: "enumerable",
w: "writable",
};
return mode.split("").reduce(
(desc, m) => {
desc[map[m]] = true;
return desc;
}, {
value,
configurable: false,
enumerable: false,
writable: false,
}
);
}
function isSymbol(value) {
if (!value) return false;
if (typeof value !== "object") return false;
if (!value.constructor) return false;
if (value.constructor.name !== "Symbol") return false;
if (!value.__SymbolData__ || value.__SymbolData__ !== value) return false;
if (value[value.constructor.toStringTag] !== "Symbol") return false;
return true;
}
function validateSymbol(value) {
if (!isSymbol(value)) return new TypeError(value + "is not a symbol");
return value;
}
const {
defineProperty,
defineProperties
} = Object;
const HiddenSymbol = function Symbol(description) {
if (this instanceof HiddenSymbol)
throw new TypeError("Symbol is not a constructor");
return SymbolPolyfill(description);
};
const SymbolPolyfill = function Symbol(description) {
if (this instanceof SymbolPolyfill)
throw new TypeError("Symbol is not a constructor");
let descString;
description === undefined ?
(descString = undefined) :
(descString = String(description));
let symbol = Object.create(HiddenSymbol.prototype);
defineProperties(symbol, {
__Description__: d("c", descString),
__Name__: d("c", generateName(descString)),
__SymbolData__: d("c", symbol),
});
return symbol;
};
defineProperties(SymbolPolyfill, {
for: d("cw", function (key) {
key = String(key);
return GlobalSymbolRegistry[key] ?
GlobalSymbolRegistry[key] :
(GlobalSymbolRegistry[key] = SymbolPolyfill(key));
}),
keyFor: d("cw", function (symbol) {
for (let key in globalSymbols) {
if (globalSymbols[key] === symbol) return key;
}
}),
prototype: d("", SymbolPolyfill.prototype),
});
Object.keys(INTERNAL).forEach((key) => {
defineProperty(SymbolPolyfill, key, d("", SymbolPolyfill(key)));
});
defineProperties(HiddenSymbol.prototype, {
constructor: d("cw", SymbolPolyfill),
toString: d("cw", function () {
return this.__Name__;
}),
});
defineProperties(SymbolPolyfill.prototype, {
toString: d("cw", function () {
return `Symbol(${validateSymbol(this).__Description__ || ""})`;
}),
valueOf: d("cw", function () {
return validateSymbol(this).__SymbolData__;
}),
});
defineProperty(
SymbolPolyfill.prototype,
SymbolPolyfill.toPrimitive,
d("c", function () {
return validateSymbol(this).__SymbolData__;
})
);
defineProperty(
SymbolPolyfill.prototype,
SymbolPolyfill.toStringTag,
d("c", "Symbol")
);
defineProperty(
HiddenSymbol.prototype,
SymbolPolyfill.toPrimitive,
d("c", SymbolPolyfill.prototype[SymbolPolyfill.toPrimitive])
);
defineProperty(
HiddenSymbol.prototype,
SymbolPolyfill.toStringTag,
d("c", SymbolPolyfill.prototype[SymbolPolyfill.toStringTag])
);
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})() // 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()
// 1
;
(function (done) {
if (!done) return
})()