zoukankan      html  css  js  c++  java
  • js小方法

    1. 保留N位小数
      const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
      // 事例
      toFixed(25.198726354, 1);       // 25.1
      toFixed(25.198726354, 2);       // 25.19
      toFixed(25.198726354, 3);       // 25.198
      toFixed(25.198726354, 4);       // 25.1987
      toFixed(25.198726354, 5);       // 25.19872
      toFixed(25.198726354, 6);       // 25.198726
    2. 检查当前是否有元素处于焦点中
      const elementIsInFocus = (el) => (el === document.activeElement);
      elementIsInFocus(anyElement)
      // 如果在焦点中返回true,如果不在焦点中返回 false
    3. 获取参数平均数值
      const average = (...args) => args.reduce((a, b) => a + b) / args.length;
      average(1, 2, 3, 4);
      // 2.5
    4. 获取文件后缀名
      /**
       * 获取文件后缀名
       * @param {String} filename
       */
       export function getExt(filename) {
          if (typeof filename == 'string') {
              return filename
                  .split('.')
                  .pop()
                  .toLowerCase()
          } else {
              throw new Error('filename must be a string type')
          }
      }
      
      getExt("1.mp4") //->mp4
    5. 复制内容导剪贴板
      export function copyToBoard(value) {
          const element = document.createElement('textarea')
          document.body.appendChild(element)
          element.value = value
          element.select()
          if (document.execCommand('copy')) {
              document.execCommand('copy')
              document.body.removeChild(element)
              return true
          }
          document.body.removeChild(element)
          return false
      }
      
      //如果复制成功返回true
      copyToBoard('lalallala')
    6. 随机生产rbg值
      function rgb(){
          let r = Math.floor(Math.random()*256);
          let g = Math.floor(Math.random()*256);
          let b = Math.floor(Math.random()*256);
          let rgb = 'rgb('+r+','+g+','+b+')';
          return rgb;
      }
    7. 随机生产16进制颜色
      function color16(){
         let r = Math.floor(Math.random()*256);
         let g = Math.floor(Math.random()*256);
         let b = Math.floor(Math.random()*256);
         let color = '#'+r.toString(16)+g.toString(16)+b.toString(16);
         return color;
      }
    8. 判断是否为质数
      const mathIsPrime = n => {
        if (n === 2 || n === 3) {
          return true
        }
        if (isNaN(n) || n <= 1 || n % 1 != 0 || n % 2 == 0 || n % 3 == 0) {
          return false;
        }
        for (let x = 6; x <= Math.sqrt(n) + 1; x += 6) {
          if (n % (x - 1) == 0 || n % (x + 1) == 0) {
            return false
          }
        }
        return true
      }
      mathIsPrime(0) // true
    9. 时间格式化
      const dateFormatter = (formatter, date) => {
          date = (date ? new Date(date) : new Date)
          const Y = date.getFullYear() + '',
                M = date.getMonth() + 1,
                D = date.getDate(),
                H = date.getHours(),
                m = date.getMinutes(),
                s = date.getSeconds()
          return formatter.replace(/YYYY|yyyy/g, Y)
                          .replace(/YY|yy/g, Y.substr(2, 2))
                          .replace(/MM/g, (M < 10 ? '0' : '') + M)
                          .replace(/DD/g, (D < 10 ? '0' : '') + D)
                          .replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
                          .replace(/mm/g, (m < 10 ? '0' : '') + m)
                          .replace(/ss/g, (s < 10 ? '0' : '') + s)
      }
      
      dateFormatter('YYYY-MM-DD HH:mm', '1995/02/15 13:55') // 1995-02-15 13:55
  • 相关阅读:
    我是新手,我在学Android
    WAMP环境下(apache2.2.21+Php5.3.10)编写PHP扩展
    java中三种字符串正则匹配方式
    PHP扩展开发过程中的问题
    Android错误解决方法之:Debug certificate expired on
    Ubuntu下C++开发PHP开发扩展的注意事项
    我的笔试题
    很多时候,是否好好看完一本好书,对一个人的提升往往能达到质的区别
    C语言常用宏定义
    Linux下用C++开发PHP扩展
  • 原文地址:https://www.cnblogs.com/wuxu-dl/p/15167334.html
Copyright © 2011-2022 走看看