常用js函数封装

  • 判断object
  • 判断undefined
  • 判断非undefined
  • 转换成字符串
  • 数组转换成对象

判断object

function isObject(obj){
    return obj !== null && typeof obj==='object'
}

判断undefined

function isUndef(v){
    return v===undefined || v===null
}

判断非undefined

function isUndef(v){
    return v!==undefined && v!==null
}

转换成字符串

function toString(val){
  return val == null? '': typeof val==='object'? JSON.stringify(val): String(val);
}

数组转换成对象

function toObject(arr){
  var res={};
  arr.forEach((item,index)=>{
    res[index]=item;
  });
  return res
}

猜你喜欢

转载自www.cnblogs.com/jingouli/p/11811201.html