js常用的一些工具類方法

2021-09-25 23:56:36 字數 3120 閱讀 8325

1,獲取瀏覽器位址列引數

function geturlparam(name)
2,位址列上使用unicode加密解密,這樣做主要是為了防止引數丟失

// 加密方法

function compilestr(code)

return escape(c);

}// 解密方法

function uncompilestr(code)

return c;

}

3,物件轉殖。解決物件是引用型別的問題。

function clone(obj)else if(obj instanceof function)else

}return temp;

}

var newobj = object.assign({}, obj); // es6的新方法

var newobj = json.parse(json.stringify(obj)); // 這種方法也能切斷物件引用,但是有個缺點:不能轉殖函式進來。

4,day.js 處理日期 並根據法定節假日往後推演。

// 計算日期的方法

captureday(enter) else

plantime = this.$dayjs(enter).add(daynum, 'day').format('yyyy-mm-dd'); // 計算出了初次上線時間

var expdaya = this.holidayfile(plantime), // 上線時間往後推演幾次,最多往後推演4次

expdayb = this.holidayfile(expdaya),

expdayc = this.holidayfile(expdayb),

expdayd = this.holidayfile(expdayc),

expdaye = this.holidayfile(expdayd),

expdayarr = [plantime, expdaya, expdayb, expdayc, expdayd, expdaye];

for (var val of expdayarr)

}return plantime;

},// 上線時間往後推算

holidayfile(ent) else

},

5,將物件屬性轉化成字典序(常用於加密)

// 將物件屬性轉為字典序

let objkeysort = function (obj) ;//建立乙個新的物件,用於存放排好序的鍵值對

for (let i = 0; i < newkey.length; i++)

return newobj;//返回排好序的新物件

}

6,將物件拼接成&連線的字串

// 拼接屬性引數

let connectstr = function (obj, token)

if (token) else

return str;

}

7,將時間轉化成yyyy-mm-dd hh-mm-ss.不傳時間戳取當前時間

let formatdatetime = function(inputtime)  else 

var y = date.getfullyear();

var m = date.getmonth() + 1;

m = m < 10 ? ('0' + m) : m;

var d = date.getdate();

d = d < 10 ? ('0' + d) : d;

var h = date.gethours();

h = h < 10 ? ('0' + h) : h;

var minute = date.getminutes();

var second = date.getseconds();

minute = minute < 10 ? ('0' + minute) : minute;

second = second < 10 ? ('0' + second) : second;

return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;

};

8,js運算

/*a乘b*/

//呼叫:accmul(arg1,arg2) 返回值:arg1乘以arg2的精確結果

function accmul(arg1,arg2)catch(e){}

trycatch(e){}

return number(s1.replace(".",""))*number(s2.replace(".",""))/math.pow(10,m)

}

/*a除b*/

function accdiv(arg1, arg2)

catch (e)

try

catch (e)

with (math)

}

// 精度計算加法

function dcmadd(arg1,arg2)catch(e)

trycatch(e)

m=math.pow(10,math.max(r1,r2));

return (accmul(arg1,m)+accmul(arg2,m))/m;

}// 精度計算減法

function dcmminus(arg1,arg2)catch(e)

trycatch(e)

m=math.pow(10,math.max(r1,r2));

return (accmul(arg1,m)-accmul(arg2,m))/m;

}

9,防抖函式

// 通過定時器實現

function debounce(fn,delay)

timer = settimeout(fn,delay) // 簡化寫法

}}

js的一些工具類

寫入cookie functionsetcookie name,value 刪除cookie functiondelcookie name 讀取cookie functiongetcookie name cookie相關 end form 表單 序列化 外掛程式 相關 beign var jquer...

一些常用js方法

使用命名空間 var global global.namespace function str else 陣列相關 判斷元素是否存在 第一種寫法 為系統陣列物件新增原型擴充套件方法 array.prototype.inarray function value return false 第二種 自定義...

一些常用的JS方法

獲取位址的引數 function search param else input type file 不可編輯 今天在做專案的時候,遇到上傳文字框的問題,在firefox中,該控制項產生的文字框是預設不可編輯的,但是在ie下,該文字框可編輯,根據使用者需要,此文字框應為不可編輯。但是直接將其設定為 ...