關鍵代碼
function isBlank(str){
if (Object.prototype.toString.call(str) ==='[object Undefined]'){//空
return true
} else if (
Object.prototype.toString.call(str) === '[object String]' ||
Object.prototype.toString.call(str) === '[object Array]') { //字條串或數(shù)組
return str.length==0?true:false
} else if (Object.prototype.toString.call(str) === '[object Object]') {
return JSON.stringify(str)=='{}'?true:false
}else{
return true
}
}
測試結果
小程序使用方法
utils.js文件
const formatTime = date => {
const year = date.getFullYear()
const mOnth= date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const secOnd= date.getSeconds()
return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
/** * 用于判斷空,Undefined String Array Object */
function isBlank(str){
if (Object.prototype.toString.call(str) ==='[object Undefined]'){//空
return true
} else if (
Object.prototype.toString.call(str) === '[object String]' ||
Object.prototype.toString.call(str) === '[object Array]') { //字條串或數(shù)組
return str.length==0?true:false
} else if (Object.prototype.toString.call(str) === '[object Object]') {
return JSON.stringify(str)=='{}'?true:false
}else{
return true
}
}
/** * 導出 */
module.exports = {
formatTime: formatTime,
String:{
isBlank: isBlank
}
}
其它js文件使用
import { String } from '../../utils/util.js';
var xx = '';
if( String.isBlank(xx))
//xx is blank
聲明:所有內容來自互聯(lián)網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯(lián)系我們進行處理。