-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
314 lines (305 loc) · 9.02 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
div {
background: red;
width: 200px;
height: 200px;
}
/* #div{
float: left;
background: green;
} */
#parent {
width: 500px;
height: 500px;
background: blue;
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
/* #children{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
} */
/* #children{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
} */
.flex-box {
display: flex;
/* flex-flow: column wrap; */
justify-content: center;
align-items: stretch;
width: 500px;
height: 500px;
background: #000;
}
#flexbox{
width: 100px;
height: 100px;
background: green;
display: flex;
justify-content: space-between;
}
#flexbox div{
width: 20px;
height: 20px;
background: red;
}
#flexbox div:nth-child(2){
align-self: flex-end;
}
.dailog {
width: 200px;
height: 200px;
margin: 200px;
overflow: hidden;
}
.text {
margin: 0;
overflow: hidden;
text-overflow: ellipsis;
line-height: 22px;
}
.text::after{
content: '确认';
}
.confrim {
display: inline;
width: 30px;
height: 10px;
background: blue;
color: white;
}
</style>
</head>
<body>
<p id="p">123123</p>
<div id="parent">
<div id="children"></div>
</div>
<section class="flex-box">
<div></div>
<div></div>
</section>
<div id="box"></div>
<div id="flexbox">
<div></div>
<div></div>
</div>
<div class="dailog">
<p class="text">
封疆大吏发卡机as登陆开发机爱丽丝快点解封拉设计费老的手机安抚辣椒水弗兰克驾驶的开发傻了的开发机死啦的恐惧
封疆大吏发卡机as登陆开发机爱丽丝快点解封拉设计费老的手机安抚辣椒水弗兰克驾驶的开发傻了的开发机死啦的恐惧
封疆大吏发卡机as登陆开发机爱丽丝快点解封拉设计费老的手机安抚辣椒水弗兰克驾驶的开发傻了的开发机死啦的恐惧
封疆大吏发卡机as登陆开发机爱丽丝快点解封拉设计费老的手机安抚辣椒水弗兰克驾驶的开发傻了的开发机死啦的恐惧
</p>
<span class="confrim">确定</span>
</div>
<script>
function myCall(context, ...args) {
const self = context || window;
self.fn = this;
const result = self.fn(...args);
delete self.fn;
return result;
}
function myBind(context, ...bindArgs) {
// 保存当前函数,因为bind需要返回一个新的函数,在新的函数里this指向会有问题
const self = this;
// 不使用原生的call方法,使用自己写的 myCall 方法
return function (...args) {
Function.prototype.myCall = myCall;
console.log([...bindArgs], [...args]);
return self.myCall(context, ...bindArgs, ...args);
};
}
Function.prototype.myBind = myBind;
let obj = {
name: 'jack',
age: 18,
};
function getAge(desc, desc1, desc2) {
console.log(desc, desc1, desc2);
return this.age;
}
let myGetAge = getAge.myBind(obj, 'myBind调用', '参数1');
console.log(getAge('正常调用', '参数1', '参数2'));
// 正常调用 参数1 参数2
// undefined
console.log(myGetAge('参数2'));
// myBind调用 参数1 参数2
// 18
function deepClone(obj) {
if (obj === null) return obj;
if (obj instanceof Date) return new Date(obj);
if (obj instanceof RegExp) return new RegExp(obj);
if (typeof obj !== 'object') return obj;
let result = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = deepClone(obj[key]);
}
}
return result;
}
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.field = 'ps';
const person = new Person('jack', 18);
// console.log(person); // {name: 'jack', age: 18}
// console.log(person.field); // ps
// // 通过构造函数创建出来的实例对象会有一个 __proto__ 属性,指向构造函数的原型对象 peototype
// // 如果自身没有这个属性则会通过原型链往上层找
// console.log(person.__proto__ === Person.prototype);
// // 构造函数的 prototype.constructor 指向 构造函数本身
// console.log(Person.prototype.constructor === Person);
// console.log(Person.__proto__ === Function.prototype);
// // 构造函数原型对象上的 __proto__ 指向 Object 的原型对象,构造函数本身也是一个对象
// console.log(Person.prototype.__proto__ === Object.prototype);
// // 对象原型上的 __proto__ 属性指向 null,原型链的顶层就是 null
// console.log(Object.prototype.__proto__ === null);
// console.log(person.__proto__.__proto__ === Object.prototype);
function promiseAll(promises) {
return new Promise((resolve, reject) => {
let length = promises.length;
let result = [];
let count = 0;
for (let i = 0; i < length; i++) {
Promise.resolve(promises[i])
.then((res) => {
result[i] = res;
count += 1;
if (count === length) {
resolve(result);
}
})
.catch((err) => {
reject(err);
});
}
});
}
const p1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(11111);
}, 3000);
});
const p2 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(22222);
}, 1000);
});
// promiseAll([p1, 123, p2])
// .then((res) => {
// console.log('then', res);
// })
// .catch((err) => {
// console.log('catch', err);
// });
// let arr = [1,2,3]
// for(let i = 0; i<arr.length; i++) {
// console.log(i)
// }
const tree = {
children: [
{
index: 0,
children: [
{
index: 1,
children: [
{
index: 3,
},
],
},
],
},
{
index: 4,
},
{
index: 5,
children: [
{
index: 7,
children: [
{
index: 8,
},
],
},
],
},
{
index: 6,
},
],
};
function dfs(node, nodeList = []) {
if (!node) return;
console.log(node.index);
nodeList.push(node);
let children = node.children || [];
children.forEach((item) => {
dfs(item, nodeList);
});
return nodeList
}
console.log(dfs(tree, []))
function dfs2(node) {
let nodes = [];
let stack = [];
if(node) {
stack.push(node);
while(stack.length) {
let cur = stack.pop();
nodes.push(cur);
let children = cur.children || [];
// 因为上面是 pop,所以这里得从最后面开始遍历 push 进去
// unshift 性能不好
for(let i = children.length - 1; i >= 0; i--) {
stack.push(children[i]);
}
}
}
return nodes;
}
console.log(dfs2(tree))
// for(var i = 0; i< 10; i++) {
// const a = document.createElement('a');
// a.innerHTML = i + '<br />'
// a.setAttribute('number', i);
// document.getElementById('box').appendChild(a);
// }
// document.getElementById('box').addEventListener('click', (e) => {
// const data = e.target
// console.log(e.target.getAttribute('number'))
// })
for(var i = 0; i< 10; i++) {
const a = document.createElement('a');
a.innerHTML = i + '<br />'
a.addEventListener('click', () => {
alert(i)
})
document.getElementById('box').appendChild(a);
}
</script>
</body>
</html>