10個實用的工具函式

2022-07-05 15:24:14 字數 3387 閱讀 5918

new array建立的陣列只是新增了length屬性,並沒有實際的內容。通過擴充套件後,變為可用陣列用於迴圈

function getweektime()
使用

getweektime()

// ["2020/2/26", "2020/2/27", "2020/2/28", "2020/2/29", "2020/3/1", "2020/3/2", "2020/3/3"]

判斷核心使用object.prototype.tostring,這種方式可以準確的判斷資料型別。

/**

* @param target

* @param type

* @return

*/function istype(target, type)

使用

istype(, 'array') // true

istype(/\d/, 'regexp') // true

istype(new date(), 'date') // true

istype(function(){}, 'function') // true

istype(symbol(1), 'symbol') // true

應用場景很簡單,當你需要使用乙個物件,但想移除部分屬性時,可以使用該方法。同樣的,你可以實現乙個物件屬性選取方法。

/**

* @param object

* @param props

* @return

*/function omit(object, props=)

object.keys(object).foreach(key=>

})return res

}

使用

let data = 

omit(data, ['id']) //

乙個很靈活的日期格式化函式,可以根據使用者給定的格式進行格式化,能應對大部分場景

/**

* @param format

* @param timestamp - 時間戳

* @return

*/function formatdate(format='y-m-d h:m', timestamp=date.now())

let formatnumber = (n) => n > 10 ? n : '0' + n

let res = format

.replace('y', dateinfo.y)

.replace('m', dateinfo.m)

.replace('d', dateinfo.d)

.replace('h', formatnumber(dateinfo.h))

.replace('m', formatnumber(dateinfo.m))

.replace('s', formatnumber(dateinfo.s))

return res

}

使用

formatdate() // "2020-2-24 13:44"

formatdate('m月d日 h:m') // "2月24日 13:45"

formatdate('h:m y-m-d', 1582526221604) // "14:37 2020-2-24"

web performance api允許網頁訪問某些函式來測量網頁和web應用程式的效能

performance.timing包含延遲相關的效能資訊

performance.memory包含記憶體資訊,是chrome中新增的乙個非標準擴充套件,在使用時需要注意

window.onload = function())

})}

效能優化方案,防抖用於減少函式請求次數,對於頻繁的請求,只執行這些請求的最後一次。

基礎版本

function debounce(func, wait = 300)

timer = settimeout(func.bind(this),wait);

}}

改進版本新增是否立即執行的引數,因為有些場景下,我們希望函式能立即執行。

/**

* @param func - 執行函式

* @param wait - 等待時間

* @param immediate - 是否立即執行

* @return

*/function debounce(func, wait = 300, immediate = false), wait)

return function(...arg)

}else

}}

使用

let scrollhandler = debounce(function(e), 500)

window.onscroll = scrollhandler

效能優化方案,節流用於減少函式請求次數,與防抖不同,節流是在一段時間執行一次。

/**

* @param func - 執行函式

* @param delay - 延遲時間

* @return

*/function throttle(func, delay), delay)

}}}

使用

let scrollhandler = throttle(function(e), 500)

window.onscroll = scrollhandler

/**

* @param data - base64字串

*/function downloadfile(filename, data) else if ( document.createeventobject ) else if (typeof downloadlink.onclick == 'function' )

document.body.removechild(downloadlink);

} }

function ispcbroswer()
function getplatforminfo()

for (const key in platforms)

}}}

PHP中的10個實用函式

這個函式可以用來檢查特定檔案中的php語法是否正確。使用方法 error message filename php script.php if php check syntax filename,error message else 參考 當你想要把php 顯示到頁面上時,highlight str...

介紹PHP中的10個實用函式

這個函式可以用來檢查特定檔案中的php語法是否正確。使用方法 error message filename php script.php if php check syntax filename,error message else 參考 當你想要把php 顯示到頁面上時,highlight str...

分享PHP中的10個實用函式

1.php check syntax 這個函式可以用來檢查特定檔案中的php語法是否正確。使用方法 error message filename php script.php if php check syntax filename,error message else 2.highlight st...