方法记

方法记:
1.判断传入的对象是否是函数

function isFunction( obj ) { return typeof obj === 'function'; }

2.去除字符串两头空格

function trim(str){return str.replace(/(^\s*)|(\s*$)/g,"");}

3.将 Date 转化为指定格式的String

/*
 * 【将 Date 转化为指定格式的String】    
 * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符      
 * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)      
 * eg:
 * pattern(new Date(),"yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423    
 * pattern(new Date(),"yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04       
 * pattern(new Date(),"yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04      
 * pattern(new Date(),"yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04     
 * pattern(new Date(),"yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18      
 */ 
function pattern(dateObj,fmt) {
    var o = {
        "M+": dateObj.getMonth() + 1, //月份       
        "d+": dateObj.getDate(), //日       
        "h+": dateObj.getHours() % 12 == 0 ? 12 : dateObj.getHours() % 12, //小时       
        "H+": dateObj.getHours(), //小时       
        "m+": dateObj.getMinutes(), //分       
        "s+": dateObj.getSeconds(), //秒       
        "q+": Math.floor((dateObj.getMonth() + 3) / 3), //季度       
        "S": dateObj.getMilliseconds() //毫秒       
    };
    var week = {
        "0": "\u65e5",
        "1": "\u4e00",
        "2": "\u4e8c",
        "3": "\u4e09",
        "4": "\u56db",
        "5": "\u4e94",
        "6": "\u516d"
    };
    if ( /(y+)/ .test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (dateObj.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if ( /(E+)/ .test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[dateObj.getDay() + ""]);
    }
    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;
};

  formatTime: function (number) {
    let n = number * 1000;
    let date = new Date(n);
    let Y = date.getFullYear() + '/';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    let H = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + ':';
    let MI = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() + ':';
    let S = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return (Y + M + D + " " + H + MI + S);
  },


4.计算2个日期之间的天数

function dayMinus(startDate, endDate){
    if(startDate instanceof Date && endDate instanceof Date){
        var days = Math.floor((endDate-startDate)/(1000 * 60 * 60 * 24));
        return days;
    }else{
        return "Param error,date type!";
    }
}

5.判断是否是中文

function isChina(str){
    var reg = /^([u4E00-u9FA5]|[uFE30-uFFA0])*$/;
    if(reg.test(str)){
        return false;
    }
    return true;
}

6.输出任意值到任意值的随机整数

function fRandomBy(under, over){ 
   switch(arguments.length){ 
     case 1: return parseInt(Math.random()*under+1); 
     case 2: return parseInt(Math.random()*(over-under+1) + under); 
     default: return 0; 
   } 
} 

7.生成随机RGB颜色

/*【生成随机RGB颜色】示例:
getColor(1, 0, 0,0.8)  颜色为红色类别的随机颜色,不透明度0.8 。
getColor(1, 0, 1,1)  颜色为紫色类别的随机颜色,不透明度1 。
getColor(1, 1, 1,0.5)  颜色为全部类别的随机颜色(全彩),不透明度0.5 。
getColor(0, 0, 0,1)  颜色为黑白颜色类别的随机颜色(黑白灰),不透明度1 。
*/

function getColor(r, g, b, a) {//输出rgba颜色格式  
  var rgb = 155;  
  var c = Math.floor(Math.random() * (255 - rgb) + rgb);  
  if (r * g * b == 1) {  
    r = Math.floor(Math.random() * 255);  
    g = Math.floor(Math.random() * 255);  
    b = Math.floor(Math.random() * 255);  
  } else if (r + g + b == 0) {  
    var t = Math.floor(Math.random() * 255);  
    r = t;  
    g = t;  
    b = t;  
  } else {  
    r = r == 1 ? (Math.floor(Math.random() * (255 - rgb) + rgb)) : (Math.floor(Math.random() * (c / 2)));  
    g = g == 1 ? Math.floor(Math.random() * (255 - rgb) + rgb) : Math.floor(Math.random() * (c / 2));  
    b = b == 1 ? Math.floor(Math.random() * (255 - rgb) + rgb) : Math.floor(Math.random() * (c / 2));  
  }  
  return "rgba(" + r + "," + g + "," + b + "," + a + ")";  
}  

8.排序

$scope.sortf = function (data) {
            for (var i = 0; i < data.length - 1; i++) {//比较的次数是length-1
                for (var j = 0; j < data.length - 1 - i; j++) {
                    if (data[j].value > data[j + 1].value) {
                        var tmp = data[j].value;
                        data[j].value = data[j + 1].value;
                        data[j + 1].value = tmp;
                    }
                }
            }
            return data;
        };

9.JSON

JSON.stringify() 用于把一个JSON对象(恩,javascript中万物皆对象),转化为一个字符串。

JSON.parse() 只拥有两个参数,第一个就是把这个字符串转换为JSON对象,第二个就是筛选对象。

10.使用对象作为函数参数(调用时参数不定,且多个)
11.合并数组

  • concat
  • for
  • reduce
  • push.apply
var arr1 = [1,2,3];
var arr2 = [4,5,6];
arr1 = arr1.concat(arr2);

reduce();coll为传入的arr1,item为遍历的arr2

var arr1=[1,2,3,4,5],arr2=[6,7,8,9,10];
arr1 = arr2.reduce( function(coll,item){
     coll.push( item );
     return coll;
 }, arr1 );
 console.log(arr1)//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

arr1.push为 function实例,实例有apply方法

var arr1=[1,2,3,4,5],arr2=[6,7,8,9,10];
arr1.push.apply(arr1,arr2);
console.log(arr1)//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

12.!!转布尔类型
具体情况具体分析
13.将参数转为数组

var _arguments=Array.prototype.slice.apply(arguments)

9-13转自守望的博客;
https://segmentfault.com/a/1190000011031658?utm_source=weekly&utm_medium=email&utm_campaign=email_weekly#articleHeader8

13.数字增加千分符

function addThousandSeparator(num) {
    return num.toString().replace(/^(-?)(\d+)(\.?\d*)$/, function(num, minus, integer, decimal) {
        return minus + integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',') + decimal
    })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值