common方法

  • 金额格式化:123456 =》 123,456
     
function formatNumber(str) {
    // str 的最大长度为16位,
    //["0", "9", "8", "7", "6", "5", "4", "3", "2", "1"]
    // str的类型可能有:number,string ,null,undefined,
    if (typeof str === 'string' || typeof str === 'number') {// 123,"123","","0","0.00",0,0.00

        // 数字没有split方法,string转化;
        var decimals = String(str).split('.')[1];//小数
        decimals = decimals ? '.' + decimals : '';
        str = String(str).split('.')[0];//整数
        var temStr = str.split("");
        //去空格,
        var tem = [];
        for (var i = 0; i < temStr.length; i++) {
            temStr[i] === " " ? null : tem.push(str[i]);
        }
        if (temStr.length) {
            // 方法一
            return temStr.reverse().reduce((prev, next, index) => {
                    return ((index % 3) ? next : (next + ',')) + prev
                }) + decimals
            // 方法二
            //正则匹配边界\B,边界后面必须跟着(\d{3})+(?!\d);
            return str.replace(/\B(?=(\d{3})+(?!\d))/g, ',')+decimals
           // 方法三
           return str.replace(/(\d)(?=(\d{3})+$)/g, "$1,");


        } else {// "",未判断值为 " "的状况
            return ''
        }
    } else {//null,undefined
        return ''
    }
}


//方法四:toLocaleString

(123456789).toLocaleString('en-US')//1,234,567,890

 

日期格式化javascript

// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 能够用 1-2 个占位符,
// 年(y)能够用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) {
    // fmt = YYYY-MM-DD HH:mm:ss 逐一替换正则中的字符
    var o = {
        "Y+":this.getFullYear(),//年份
        "M+": this.getMonth() + 1, //月份
        "D+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "Q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(Y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    return fmt;
}
  • 数组去重(注:暂不考虑对象字面量,函数等引用类型的去重,也不考虑NaN,undefined,null等特殊类型状况)
//方法一:

//两层循环,O(n^2)

//方法二:

function unique(arr) {
    return arr.filter(function (ele, index, array) {
        return array.indexOf(ele) === index
        //很巧妙,这样筛选一对一的,过滤掉重复的
    })
}
//缺点:不支持IE9如下的浏览器,时间复杂度仍是O(n^2)

//方法三:

function unique(arr) {
    var obj = {}
    return arr.filter(function (item, index, array) {
        return obj.hasOwnProperty(typeof item + item) ?
            false : (obj[typeof item + item] = true)
    })
}

//优势:hasOwnProperty是对象的属性(名称)存在性检查方法。对象的属性能够基于Hash表实现,所以对属性进行访问的时间复杂度能够达到O(1);

//filter是数组迭代的方法,内部仍是一个for循环,因此时间复杂度是O(n)。

//缺点:不兼容IE9如下浏览器,其实也好解决,把filter方法用for循环代替或者本身模拟一个 filter 方法。

//方法四:

//ES6 提供了新的数据结构 Set。它相似于数组,可是成员的值都是惟一的,没有重复的值。

const unique=a=>[...new Set(a)]

//优势:ES6语法,简洁高效,咱们能够看到,去重方法从原始的14行代码到ES6的1行代码,其实也说明了JavaScript这门语言在不停的进步,相信之后的开发也会愈来愈高效。

//缺点:兼容性问题,现代浏览器才支持,有babel这些都不是问题。

 

canvas 画圆java

function docanves() {
    var dashBoard = document.getElementById('dashBoard');
    var ctx = dashBoard.getContext('2d');
    ctx.beginPath();
    ctx.strokeStyle = 'rgba(255,236,202,0.7)';
    //以canvas中的坐标点(100,100)为圆心,绘制一个半径为50px的圆形
    var startArc = Math.PI * 5 / 6,
        endArc = startArc + Math.PI * 4 / 3,
        fontSize = window.fontSize;// rem
    ctx.strokeStyle = '#fff';//线 的颜色
    ctx.lineWidth = 0.05 * fontSize;// 线的宽度
    ctx.setLineDash([5, 5]);// 点状样式
    ctx.arc(150, 50, 50, startArc, endArc, false); //false 顺时针
    ctx.fillStyle = "white";// 填充颜色
    ctx.fill();
    ctx.stroke();
    ctx.closePath();
}

 

获取地址栏参数:canvas

  1. search
    //方法一
    function getQueryString(name,hash) {// search hash
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var s = hash == 'hash' ? window.location.hash : window.location.search;
        s = s ? s.substr(1) : '';
        var r = s.match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }
    
    //方法二
    function GetRequest(hash) {// search hash
        //获取url中"?"符后的字串
        var url = hash == 'hash' ? window.location.hash : window.location.search;
        var theRequest = new Object();
        var str = url && url.substr(1);
        if ((url.indexOf("?") != -1) && str) {
            str = str.split("&");
            for (var i = 0; i < str.length; i++) {
                theRequest[str[i].split("=")[0]] = unescape(str[i].split("=")[1]);
            }
        }
        return theRequest;
    }
相关文章
相关标签/搜索