最近要干的活

promise.all

promise执行过程

1
2
3
4
5
6
7
8
9
10
// promise较好的格式
promise
.then(function(data) { //cb
// success
})
.catch(function(err) {
// error
});

catch === then(null,(reject)=>{})

promise串行执行

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
const timeout = ms => new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms);
});

const ajax1 = () => timeout(2000).then(() => {
console.log('1');
return 1;
});

const ajax2 = () => timeout(1000).then(() => {
console.log('2');
return 2;
});

const ajax3 = () => timeout(2000).then(() => {
console.log('3');
return 3;
});

const mergePromise = ajaxArray => {
// 在这里实现你的代码
var data = [];
var sequence = Promise.resolve();
ajaxArray.forEach(function(item){
sequence = sequence.then(item).then(function(res){
data.push(res);
return data;
});
})
return sequence;
};

mergePromise([ajax1, ajax2, ajax3]).then(data => {
console.log('done');
console.log(data); // data 为 [1, 2, 3]
});

json.stringify()与json.parse()实现

bind实现

1
2
3
4
5
6
7
8
9
10
11
Function.prototype.Bind = function (context) {
var self = this;
//获取Bind函数从第二个参数到最后一个参数
var args = Array.prototype.slice.call(arguments,1);

return function(){
//获取调用Bind时传入的参数
var bindArgs = Array.prototype.slice.call(arguments);
return self.apply(context,args.concat(bindArgs));
}
}

归并排序

二叉树遍历

动态规划

二叉排序树

二分查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function binSearch(arr,target,startIdx,endIdx) {
let len = arr.length,
startIndex = typeof startIdx === 'number'?startIdx:0,
endIndex = typeof endIdx === 'number'?endIdx:len-1,
midIndex = Math.floor((startIndex+endIndex)/2),
midVal = arr[midIndex];
if(startIndex>endIndex)return;
if(midVal === target){
return midIndex;
}else if(midVal>target){
return binSearch(arr,target,startIndex,midIndex-1);
}else{
return binSearch(arr,target,midIndex+1,endIndex);
}
}
console.log(binSearch(arr,23));