1,經過 constructor 判斷css
function isArray(value) { return value && typeof value == 'object' && value.constructor === Array }
2,經過 instanceof 判斷判斷ios
function isArray(value) { return value && typeof value == 'object' && value instanceof Array }
3,經過 toString 判斷web
function isArray(value) { return Array.isArray(value) || (typeof value == 'object' && Object.prototype.toString.call(value) === '[object Array]') }
4,isArray 原生方法判斷正則表達式
Array.isArray()
function isObject(value) { return value != null && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Object]' }
1,判斷是否安卓數組
function isAndroid() { return /Android/i.test(navigator.userAgent) || /Linux/i.test(navigator.appVersion); }
2,判斷是否ios微信
function isIOS() { return (/ipad|iphone/i.test(navigator.userAgent)); }
3,判斷是不是Safariapp
function isSafari() { return (/msie|applewebkit.+safari/i.test(navigator.userAgent)); }
4,判斷是否在微信dom
function isWeixin() { return /MicroMessenger/i.test(navigator.userAgent); }
5,判斷是否爲移動端iphone
function isMobile() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobi/i.test(navigator.userAgent) || screen.width < 500; }
/** * 按需加載js * @param {*} path */ function delayLoadJS(path) { if (!path) { return Promise.reject(); } return new Promise((resolve, reject) => { let dom = null; const scripts = document.querySelectorAll('script'); for (let item of scripts) { if (new RegExp(path).test(item.src)) { dom = item; onload(dom, () => resolve()); return; } } if (!dom) { const script = document.createElement('script'); script.src = path; script.onload = () => { resolve(); script.onload = null; }; script.onerror = () => reject(); document.body.appendChild(script); } }); }
/** * 按需加載css * @param {*} path */ function delayLoadCSS(path) { if (!path) { return Promise.reject(); } return new Promise((resolve, reject) => { let dom = null; let links = document.querySelectorAll('link'); for (let item of links) { if (new RegExp(path).test(item.href)) { dom = item; onload(dom, resolve); return; } } if (!dom) { const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = path; link.onload = () => { resolve(); link.onload = null; }; link.onerror = () => reject(); document.head.appendChild(link); } }); }
/** * 解決同時異步加載同一個文件的問題 * @param {*} dom * @param {*} resolve */ function onload(dom, resolve) { const oldOnload = dom.onload; if (oldOnload) { dom.onload = () => { oldOnload(); resolve(); }; } else { resolve(); } }
1,郵箱異步
function isEmail(email) { var r = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; return r.test(email); }
2,QQ
function isQQ(qq) { var r = /^[1-9]\d{4,10}$/; return r.test(qq); }
3,網址
function isUrl(str) { return /^(((ht)tps?):\/\/)?[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:\/~+#]*[\w\-@?^=%&\/~+#])?$/.test(str); }
4,身份證
function isCard(str) { return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str); }
5,漢字
function isChinese(str) { return /^[\u4e00-\u9fa5]+$/.test(str); }
6,英文
function isEnglish(str) { return /^[a-zA-Z]*$/.test(str); }
1,對象
// 簡單粗暴,一步到位 JSON.parse(JSON.stringify(obj));
2,數組
能夠用Array.slice(),Array.concat(),ES6擴展運算符...等方法來實現
以上大概是目前來講項目中運用較多的一些公用方法,可能實現方法不是最好的,這裏僅供參考。還有許多通用的方法,這裏只想起這麼多了,之後再慢慢補充吧。