util笔记

1. arr swap

1
2
3
4
5
6
Array.prototype.swap = function(lIndex,rIndex){
var temp = this[rIndex];
this[rIndex] = this[lIndex];
this[lIndex] = temp;
return this;
}

或者用call

1
2
3
4
5
6
7
8
swap.call(nums,baseIndex,targetIndex);
function swap(i,j){
var temp;
temp = this[j];
this[j] = this[i];
this[i] = temp;
return this;
}

2. bubbleSort

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function bubbleSort(){
var swap = false;
var arr = this.queue,
lastIndexOfNotSortedArr = arr.length-1;
do{
swap = false;
for(var i=1;i<=lastIndexOfNotSortedArr;i++){
if(arr[i-1]>arr[i])
{
arr.swap(i-1,i);
swap = true;
}
}
lastIndexOfNotSortedArr -=1;
}while(swap)
}

3. removeDuplicate

用hash

1
2
3
4
5
6
7
function removeDuplicate(){
var hashDict = {};
this.queue.forEach(function(data){
hashDict[data] = true;
});
return Object.keys(hashDict);
},

4. 日期格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function format(time,fmt){
let dateObj = new Date(parseInt(time));
var o = {
"M+": dateObj.getMonth() + 1, //月份
"d+": dateObj.getDate(), //日
"h+": dateObj.getHours(), //小时
"m+": dateObj.getMinutes(), //分
"s+": dateObj.getSeconds(), //秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (dateObj.getFullYear() + "").substr(4 - RegExp.$1.length));
for (let k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}

5. push dynamic arr to arr

楼主最近在刷leetcode,递归中会有不断push数组到result数组中的问题。但是碍于js的引用传值…结果经常就是一溜的同样的数组。微笑脸.jpg
需要复制。。

1
2
3
var a = [].concat(arr);
es6就直接这样好了:
b = [...arr];

6. find target in a sorted array

如果有则返回第一次出现的位置,如果不存在,则返回的是它如果要插入的话应该在的位置。leetcode 二分查找相关题目应用。

1
2
3
4
5
6
7
8
9
10
11
12
function searchIndex(target1){
var i = 0,
j = nums.length; //attention!
while (i < j) {
var mid = Math.floor((i + j) / 2);
if (nums[mid] < target1)
i = mid + 1; //i是最左边的===target的元素
else
j = mid;
}
return i;
}

需要注意的是:

  • j的初始值应该是nums.length而不是nums.length-1
  • 其次。循环终止条件不能是 while(i<=j) 因为终止条件中i = mid。而循环中有j = mid。如果while(i<=j)的话,就会导致死循环。

7. 数组应用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function newObj(){
var Constructor = [].shift.call(arguments);
var that = Object.create(Constructor.prototype);
Constructor.apply(that,arguments);
return that;
}
var People = function(name,age){
this.name = name;
this.age = age;
}
People.prototype = {
getName: function(){
return this.name
},
getAge: function(){
return this.age;
}
}
var luchen = newObj(People,"luchen","23");


划重点:
其实跟我们平时用构造函数差不多。觉得比较好的是newObj里面的实现。-
补充下:

1
2
3
4
5
6
// 类似于arraylikeObj.slice()
var arr = Array.prototype.slice.call(arraylikeObj);
//arguments.shift()第一个出队列
var argument1 = [].shift.call(arguments)
var leftArguments = arguments

8. 闭包实现单例

利用的就是闭包可以一直访问外部变量,而外部变量也因为闭包的引用持久的存在于内存中。

1
2
3
4
5
6
7
const singleton = (function(){
let param;
return function(initialValue){
return param || (param = initialValue);
};
})();
singleton({someKey:value}) === singleton({someKey:value})

还有一种更彻底的高阶函数的方式:

高阶函数是至少满足以下条件的函数:
参数为函数
返回值为函数

1
2
3
4
5
6
7
8
9
10
var getSingle = function(fn){
var ret;
return function(){
return ret || ret = fn.apply.call(arguments);
}
}
var getScript = getSingle(function(){
return document.createElement('script');
})
getScript() === getScript()