前端开发过程中常见的变量处理

// 判断一个变量是否是数组或者对象:
// JSON.stringify(obj) === ‘{}’;
// JSON.stringify(obj) === ‘[]’;
isNull(obj) {
for (var key in obj) {
return false;
}
return true;
}
// 判断是否是空数组
var arr = [];
if (Array.isArray(arr) && arr.length === 0) {
console.log(‘是空数组’);
}
typeof(变量) 有局限性只能粗略的分辨string,number,underfind,boolear 和function

可以使用instanceof进行判断但是要先检查 是否为:undefined和null。它能检测arr,json,function,data,reg,error

下面三个几乎可以检测所有的类型
constructor console.log( func.constructor==Function)
toString.call console.log(Object.prototype.toString.call(num),
$.type(jquery带的一个方法) console.log( $.type(num))

//深克隆vue中常用
deepClone(currobj){
if(currobjnull){
return null
}
if(typeof currobj !
‘object’){
return currobj;
}
if(currobj instanceof Array){
var newobj = [];
}else{
var newobj = {}
}
for(var key in currobj){
if(typeof currobj[key] !== ‘object’){
// 不是引用类型,则复制值
newobj[key] = currobj[key];
}else if(currobj[key]===null){
newobj[key] = null;
}else{
// 引用类型,则递归遍历复制对象
newobj[key] = this.deepClone(currobj[key])
}
}
return newobj
},

  //时长
  duration_trans(startTime, endTime) {
    var remainTime = endTime - startTime;
    // remainTime = remainTime / 3600
    if(endTime==0){
      return "永久课"
    }
    var hour = Math.floor(remainTime / 3600);
    var min = Math.floor(remainTime / 60) % 60;
    return hour + "小时" + min + "分钟"
  },
  //录制时长
  record_duration(duration) {
    return parseInt(duration / 3600) + '小时' + parseInt(duration % 3600 / 60) + '分钟' + parseInt(duration % 60) + '秒';
  },
  /*时间过滤 */
  datefilter: function (time) {
    if (time == 'null' || time == '' || time == 0 || time == 'undefinde' || isNaN(parseInt(time))) return time;
    if (time) time = time.toString();
    if (time) time.length == 13 ? time = +time : time.length == 19 ? time = time / 1000000 : time = (time * 1000);
    var date = new Date(time);
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate();
    var h = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    D = D.toString();
    h = h.toString();
    m = m.toString();
    s = s.toString();
    D.length == 1 ? D = '0' + D : D = D;
    h.length == 1 ? h = '0' + h : h = h;
    m.length == 1 ? m = '0' + m : m = m;
    s.length == 1 ? s = '0' + s : s = s;
    return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
  },
  // 时间过滤到天
  dayfilter: function (time) {
    if (time == 'null' || time == '' || time == 0 || time == 'undefinde' || isNaN(parseInt(time))) return time;
    if (time) time = time.toString();
    if (time) time.length == 13 ? time = +time : time.length == 19 ? time = time / 1000000 : time = (time * 1000);
    var date = new Date(time);
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate();
    D = D.toString();
    D.length == 1 ? D = '0' + D : D = D;
    return Y + '-' + M + '-' + D;
  },
  /*中国标准时间转时间戳 */
  chinaTimeTransTime(date) {
    if (!date) return
    let result = new Date(date).getTime();
    return result;
  },

猜你喜欢

转载自blog.csdn.net/qq_42894094/article/details/88572069