• JavaScript的优化小技巧


    1.if多条件判断

    if (x === 'abc' || x === 'def' || x === 'ghi')
    // 简化
    if(['abc','def','ghi'].includes(x))

    2.if...else

    let x
    if (x > 10) {
        test = true
    } else {
        test = false
    }
    
    // 简介
    let test = x > 10

     3.给多个变量赋值

    let a, b, c;
    a = 5;
    b = 10;
    c = 15;
    // 简写
    let [a,b,c]=[5,10,15]

    4.与短路运算符

    if (isLoggedin) {
        go()
    }
    // 简写
    isLoggedin && go()

    5.交换两个变量:平常我们都是使用三个变量来进行交换两个变量的值,在这里我们可以使用数组解构来交换两个变量的值

    let x = 'hello',
        y = 55
    const temp = x
    x = y
    y = temp
    
    // 简写
    ;[x, y] = [y, x]

     6.自定义深拷贝的方法

    var common_deepClone = function deepClone(obj) {
      // 将object原型上的toString赋值给_toString
      var _toString = Object.prototype.toString; // null, undefined, non-object, function
      // 如果传递进来的对象为空,或者传进来的参数不是对象,直接返回该对象
      if (!obj || _typeof(obj) !== 'object') {
        return obj;
      } 
      
      // DOM Node
      // object.nodeType返回的是节点的类型,如元素,属性,注释
      // 如果他是dom节点类型并且它可以被复制
      if (obj.nodeType && 'cloneNode' in obj) {
        return obj.cloneNode(true);
      } 
      
      // Date
      // 如果判断的是时间类型,就返回一个新的时间
      if (_toString.call(obj) === '[object Date]') {
        return new Date(obj.getTime());
      } 
    
      // RegExp
      // 如果返回的是正则表达式
      if (_toString.call(obj) === '[object RegExp]') {
        var flags = [];
    
        if (obj.global) {
          flags.push('g');
        }
    
        if (obj.multiline) {
          flags.push('m');
        }
    
        if (obj.ignoreCase) {
          flags.push('i');
        }
    
        return new RegExp(obj.source, flags.join(''));
      }
    
      // 如果参数是数组类型就设置一个空的数组,否则就判断对象的构造器类型,然后根据他的构造器来创建一个新的对象(可能是自定义的对象)
      var result = Array.isArray(obj) ? [] : obj.constructor ? new obj.constructor() : {};
    
      for (var key in obj) {
        result[key] = deepClone(obj[key]);
      }
      // 返回结果
      return result;
    };

    7,解构对象给另一个对象

    /**
     * 解构对象给另一个对象
     * @param {Oject} obj 赋值对象
     * @param {Oject} data 解构对象
     * @param {Array} arr 解构参数
     */
    var parseObj = function parseObj(obj, data, arr) {
      // let obj = {}
      arr.forEach(function (item) {
        data[item] ? obj[item] = data[item] : '';
      });
    };

    8.时间戳转换为  yyyy-MM-dd日期格式

    /**
     * 时间戳转换为  yyyy-MM-dd日期格式
     * @param timestamp 时间戳
     * 注意! 项目需要,只精确到月份
     */
    export function timestampToTime (timestamp) {
      var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
      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();
    
      // return Y + M + D + h + m + s;
      // 暂时只需要 年月日
      return Y + M;
    }
  • 相关阅读:
    25款有用的桌面版博客编辑器
    iOS开发- &quot;duplicate symbol for architecture i386&quot; 解决的方法
    中国眼下拥有的人造卫星的种类及其作用
    深入浅出JMS(一)——JMS简单介绍
    android之【本地通知Notification】
    蓝牙设计
    html5中的容器标签和文本标签
    amaze ui中的icon button
    amaze ui表格斑马纹效果
    amaze ui响应式表格
  • 原文地址:https://www.cnblogs.com/zhilili/p/14394197.html
Copyright © 2020-2023  润新知