-
Notifications
You must be signed in to change notification settings - Fork 0
/
test02.js
55 lines (40 loc) · 1.05 KB
/
test02.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
function test01 () {
const myInstanceOf = (target, origin) => {
let proto = Object.getPrototypeOf(target);
while (proto) {
if (proto === origin.prototype) return true
proto = Object.getPrototypeOf(proto);
}
return false
}
console.log(myInstanceOf({}, Object))
console.log({} instanceof Object)
}
function test02 () {
const remoteAdd = async (a, b) => new Promise(resolve => {
setTimeout(() => resolve(a + b), 1000);
});
// 请实现本地的localAdd方法,调用remoteAdd,能最优的实现输入数字的加法
async function localAdd (...inputs) {
// 你的实现
console.log(inputs)
const arr = [...inputs]
console.log('arr', arr)
// [ 3, 5, 2 ]
let res = 0
for (let i = 0; i < arr.length; i++) {
res = await remoteAdd(arr[i], res)
}
return res
}
// 请用示例验证运行结果:
localAdd(1, 2)
.then(result => {
console.log(result); // 3
});
localAdd(3, 5, 2)
.then(result => {
console.log(result); // 10
});
}
test01()