vue源碼中的檢測方法的實(shí)現(xiàn)
判斷是否為undefined或null
const isDef = (v) => { return v !== undefined && v !== null }
判斷是否為Promise 函數(shù)
const isPromise = (val) => { return ( val !== undefine && typeof val.then === 'function' && typeof val.catch === 'function' ) }
判斷是否為簡單數(shù)據(jù)類型
const isPrimitive (value) => { return ( typeof value === 'string' || typeof value === 'number' || typeof value === 'symbol' || typeof value === 'boolean' ) }
嚴(yán)格檢查復(fù)雜數(shù)據(jù)類型
const isPlainObject = (obj) => { return Object.prototype.toString.call(obj) === '[object Object]' } const isRegExp = (v) => { return Object.prototype.toString.call(v) === '[object RegExp]' }
將駝峰字符串轉(zhuǎn)成連接符 magicEightTall 轉(zhuǎn)換成 magic-eight-tall
const hyphenateRE = /\B([A-Z])/g const hyphenate = (str) => { return str.replace(hyphenateRE, '-$1').toLowerCase() }
將連接符轉(zhuǎn)成駝峰字符串 magic-eight-tall 轉(zhuǎn)換成 magicEightTall
const camelizeRE = /-(\w)/g const camelize = (str) => { return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') }
如果不想重復(fù)轉(zhuǎn)換,可用以下方法調(diào)用轉(zhuǎn)換函數(shù)
const cached = (fn) => { const cache = Object.create(null) console.log(cache); return ((str) => { const hit = cache[str] return hit || (cache[str] = fn(str)) }) };
例
const camelize = cached((str) => { return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '') })
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue+element使用動態(tài)加載路由方式實(shí)現(xiàn)三級菜單頁面顯示的操作
這篇文章主要介紹了vue+element使用動態(tài)加載路由方式實(shí)現(xiàn)三級菜單頁面顯示的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08vue通過數(shù)據(jù)過濾實(shí)現(xiàn)表格合并
這篇文章主要為大家詳細(xì)介紹了vue通過數(shù)據(jù)過濾實(shí)現(xiàn)表格合并,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07VUE+jszip如何實(shí)現(xiàn)下載多個文件導(dǎo)出為一個zip格式
這篇文章主要介紹了VUE+jszip如何實(shí)現(xiàn)下載多個文件導(dǎo)出為一個zip格式方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vue啟動后請求后端接口報(bào)ERR_EMPTY_RESPONSE錯誤的解決
這篇文章主要介紹了vue啟動后請求后端接口報(bào)ERR_EMPTY_RESPONSE錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05