js - 驼峰命名

1.

// 驼峰命名
    console.log(hump('border-bottom-color'))
    function  hump( str) {
        if (typeof str != 'string') return;
        var arr = str.split("-");
        var newStr = arr[0];
        for(var i = 1; i < arr.length; i++) {
            newStr += arr[i].charAt(0).toLocaleUpperCase() + arr[i].substring(1);
        }
        return newStr;
    }

    console.log(humpReg("border-bottom-color-size"))
    function humpReg(str) {
        var re = /\-(\w)/g;
        str = str.replace(re, function (s) {
            return s.slice(1).toUpperCase();
        })
        return str;
    }

 

posted @ 2016-11-30 08:59  黑土白云  阅读(890)  评论(0编辑  收藏  举报