-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
159 lines (122 loc) · 3.21 KB
/
script.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
"use strict";
const root = document.getElementById("divDepth");
console.log(root);
/**
* @param {HTMLElement} element
* @param {number} level
* @returns {number}
*/
const maxDepth = (element, level) => {
if (element.childElementCount === 0) return level;
return Math.max(...Array.from(element.children).map((el) => maxDepth(el, level + 1)));
};
console.log(maxDepth(root, 0));
const checkCalls = () => {
setTimeout(() => {
console.log("check 1");
}, 0);
const a = new Promise((resolve) => {
console.log("promise...");
resolve(3);
console.log("promise... 2");
});
a.then((val) => console.log("check " + val));
const id = setInterval(() => {
console.log("check 5");
const c = new Promise((resolve) => {
b.then((val) => {
console.log("check inside: " + val);
resolve(val);
});
});
c.then((val) => console.log("check " + val));
}, 1000);
setTimeout(() => {
clearInterval(id);
console.log("check 6");
}, 3000);
const b = new Promise((resolve) => {
resolve(4);
});
b.then((val) => console.log("check " + val));
console.log("check 7");
};
document.getElementById("check-calls").addEventListener("click", checkCalls);
const promiseLoop = () => {
const targetCount = 20000;
let count = 0;
const handlePromise = (promise) => {
promise
.then((val) => {
count++;
console.log("promise " + val);
})
.finally(() => {
if (count < targetCount) handlePromise(promise);
});
};
const promise = new Promise((resolve) => {
setTimeout(() => {
resolve(1);
}, 1000);
});
handlePromise(promise);
};
document.getElementById("promise-loop").addEventListener("click", promiseLoop, true);
const fu = function () {
const a = 1;
console.log(a);
console.log(this);
};
fu();
localStorage.setItem("test1", "123");
sessionStorage.setItem("test2", "321");
const withDelay = (func, timeout) => {
return (...args) =>
new Promise((resolve) => {
setTimeout(() => resolve(func(...args)), timeout);
});
};
function loops() {
const obj1 = {
a: 123,
b: 12,
c: 23,
d: 15,
};
Object.defineProperty(obj1, "d", { value: obj1.d, enumerable: false });
for (const key in obj1) {
console.log(key);
}
const func = withDelay((a) => {
console.log(Object.getOwnPropertyDescriptors(a));
}, 600);
Object.entries(obj1).forEach(async (item, index) => {
console.log(`${index} started`);
await func(item);
console.log(`${index} ended`);
});
(async () => {
await Promise.all([func(obj1.a), func(obj1.b)]);
})();
}
loops();
const someObj = {
a: 1,
f: () => {
return this.a;
},
f2: function () {
return this.a;
},
};
const secondObj = {
a: 2,
f: someObj.f,
f2: someObj.f2,
};
console.log(someObj.f());
console.log(someObj.f2());
console.log(secondObj.f());
console.log(secondObj.f2());
console.log(secondObj.f.bind({ a: 3 })());