JS工具函数库(持续更新)

//数组去重1

Array.prototype.removal = function () {

var newArr = [];

var obj = {};

for (var i = 0; i < this.length; i++) {

if (!obj[this[i]]) {

obj[this[i]] = 'abc';

newArr.push(this[i]);

}

}

return newArr;

};

//数组去重2

var newArr = arr.filter(function (item,index,self) {

return self.indexOf(item) === index;

})

//深拷贝

Object.prototype.deepClone = function (target, origin) {

var target = target || {};

for (var prop in origin) {

if (origin.hasOwnProperty(prop)) {

if (typeof (origin[prop]) === 'object') { //如果是引用值

if (Object.prototype.toString.call(origin[prop]) === '[object Array]') {

//如果是数组引用值

target[prop] = [];

} else { //如果是对象引用值

target[prop] = {};

}

deepClone(target[prop], origin[prop]);

} else { //如果是原始值

target[prop] = origin[prop];

}

}

}

return target;

};

//通过JSON实现深拷贝(会忽略掉函数)

function deepclone (target,origin) {

target = JSON.parse(JSON.stringify(origin));

return target;

}

//返回字节长度

Object.prototype.retBytes = function () {

var len = this.length;

for (var i = 0; i < len; i++) {

if (this.charCodeAt(i) >= 255) {

len++;

}

}

return len;

};


 

//冒泡排序

Array.prototype.bubSort = function(){

var len = this.length,

temp;

for(var i = 0; i < len; i++){

for(var j = i+1; j < len; j++){

if(this[i] < this[j]){

temp = this[i];

this[i] = this[j];

this[j] = temp;

}

}

}

return this;

};

//快速排序

        Array.prototype.quickSort = function () {

            if (this.length < 2) {

                return this;

            }

            var left = [],

                right = [],

                radix = this[0];

            this.shift();

            for (var i = 0, len = this.length; i < len; i++) {

                if (radix > this[i]) {

                    left.push(this[i]);

                } else {

                    right.push(this[i]);

                }

            }

            return [].concat(left.quickSort(), radix, right.quickSort());

        };


 

//二分查找

Array.prototype.twoPoints = function (low,high,n) {

var center = Math.floor((low+high)/2);

if(this[center] == n){

return center;

}else if(this[center] > n){

this.twoPoints(low,center-1,n);

}else if(this[center] < n){

this.twoPoints(center+1,this.length-1,n);

}

}

//斐波那契数列第n位

function fibonacci(n){

if(n == 1|| n == 2){

return 1;

}

return fibonacci(n - 1) + fibonacci(n - 2);

}

//n的阶乘

function factorial(n){

if(n == 1){

return 1;

}

return n * factorial(n - 1);

}

//速度框架

(function(){

var requestAnimationFrame = window.requestAnimationFrame || function(fn){

return setTimeout(fn,1000/60);

};

var cancelAnimationFrame = window.cancelAnimationFrame||clearTimeout;

function move(ele,attr,targetVal,speed){

var CSSdom = ele.currentStyle || getComputedStyle(ele),

startVal = parseFloat(CSSdom[attr]) || 0,

targetVal = parseFloat(targetVal),

bool = targetVal > startVal,

speed = bool? speed : -speed;

function m(){

startVal += speed;

var ifEnd = bool ? startVal >= targetVal : startVal <= targetVal;

ifEnd && (startVal = targetVal);

ele.style[attr] = startVal + 'px';

!ifEnd && requestAnimationFrame(m);

}

requestAnimationFrame(m);

}

window.move = move;

}());

        //碰撞检测

        function isCollision(ele1, ele2) {

            var L1 = ele1.offsetLeft,

                U1 = ele1.offsetTop ,

                R1 = ele1.offsetLeft + ele1.clientWidth,

                B1 = ele1.offsetTop + ele1.clientWidth,

                L2 = ele2.offsetLeft,

                U2 = ele2.offsetTop,

                R2 = ele2.offsetLeft + ele2.clientWidth,

                B2 = ele2.offsetTop + ele2.clientHeight;

            if (R1 >= L2&& B1 >= U2 && L1 <= R2 && U1 <= B2) {

                return true;

            } else {

                return false;

            }

        }

猜你喜欢

转载自blog.csdn.net/qq_36379070/article/details/81047105
今日推荐