• 【JavaScript学习笔记】函数、数组、日期


    一、函数

    一个函数应该只返回一种类型的值。

    函数中有一个默认的数组变量arguments,存储着传入函数的所有参数。

    为了使用函数参数方便,建议给参数起个名字。

    function fun1(obj, name, value){
        console.log(arguments);
        console.log(obj);
        console.log(name);
        console.log(value);
    }
    fun1({'id':12}, 'username', '张三');

    二、数组

    2.1、定义方式

    var arr = [1,2,3];

    var arr = new Array("a", "b", "c");

    数组具有length属性,既可以获取 arr.length ,也可以设置  arr.length = 2; (设置数组长度,超出数组长度的元素被丢弃)

    2.2、数组方法

    pop() 取出并删除数组末尾元素

    push(variable) 在数组的末尾追加元素

    shift() 取出并删除数组开头元素

    unshift(variable) 在数组的开头添加元素

    splice(start, length, [var1, ...]); 从arr[start]元素起,删除length个元素,然后把后面的参数插入(相当于替换)

    var arr = [1,3,5];
    arr.splice(1, 2, 2, 3, 4);    //从1个元素(即arr[1])开始,删除2个元素(即arr[1]和arr[2]),插入 2,3,4
    console.log(arr);    //[1,2,3,4]

    join(str) 数组元素以str连接返回字符串

    sort() 数组默认把元素作为字符串自然排序,也可以传入函数作为参数自定义排序规则。

    var arr = [3,5,2,1,12];
    arr.sort(function(n1, n2){
        return n1-n2;    //返回true,则n1和n2交换位置,使n1在后,n2在前,false则不变。升序,return n2-n1;则降序
    });
    console.log(arr);    //[1, 2, 3, 5, 12]

    三、日期

    var oDate = new Date();
    
    oDate.getFullYear();  //获取年份
    oDate.getMonth();    //获取月份,月份从0开始,即0表示1月,1表示2月,以此类推
    oDate.getDate();    //获取当前月份的第几日
    oDate.getHours();    //
    oDate.getMinutes();  //
    oDate.getSeconds();  //
     1 /**
     2  * 扩展Date对象的功能
     3  * @param format
     4  * @returns
     5  * @example  (new Date()).format('yyyy-MM-dd hh:mm:ss')    结果是 2018-08-13 08:42:34 这样的格式 ;
     6  */
     7 Date.prototype.format = function (format) {
     8     var o = {
     9         "M+": this.getMonth() + 1, // month
    10         "d+": this.getDate(), // day
    11         "h+": this.getHours(), // hour
    12         "m+": this.getMinutes(), // minute
    13         "s+": this.getSeconds(), // second
    14         "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
    15         "S": this.getMilliseconds()
    16         // millisecond
    17     }
    18     if (/(y+)/.test(format)) {
    19         format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    20     }
    21     for (var k in o) {
    22         if (new RegExp("(" + k + ")").test(format)) {
    23             format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    24         }
    25     }
    26     return format;
    27 }
    28 
    29 /**
    30  * 扩展Date对象的功能
    31  * 获取上一个月日期对象
    32  * @param date
    33  * @returns {Date}
    34  */
    35 Date.prototype.getPreMonth = function() {
    36     var year = this.getFullYear();
    37     var month = this.getMonth()+1;
    38     var day = this.getDate();
    39     var hour = this.getHours();
    40     var minute = this.getMinutes();
    41     var second = this.getSeconds();
    42     
    43     var y = year;
    44     var m = parseInt(month) - 1;
    45     if (m == 0) {    // 如果是一月
    46         y = parseInt(y) - 1;
    47         m = 12;
    48     }
    49     
    50     var monthDays = new Date(y, m, 0).getDate(); //获取 m 月的天数
    51     var d = day;
    52     if (d > monthDays) {
    53         d = monthDays;
    54     }
    55     return new Date(y, m-1, d, hour, minute, second);
    56 }
    57 
    58 
    59 /**
    60  * 扩展Date对象的功能
    61  * 获取下一个月的日期
    62  * @param date
    63  * @returns {Date}
    64  */
    65 Date.prototype.getNextMonth = function(){
    66     var year = date.getFullYear();
    67     var month = date.getMonth()+1;
    68     var day = date.getDate();
    69     var hour = date.getHours();
    70     var minute = date.getMinutes();
    71     var second = date.getSeconds();
    72     
    73     var y = year;
    74     var m = parseInt(month) + 1;
    75     if (m == 13) {
    76         y = parseInt(y) + 1;
    77         m = 1;
    78     }
    79     
    80     var d = day;
    81     var monthDays = new Date(y, m, 0).getDate(); // 获取 m 月的天数
    82     if (d > monthDays) {
    83         d = monthDays;
    84     }
    85     return new Date(y, m-1, d, hour, minute, second);
    86 }
  • 相关阅读:
    http响应状态码大全
    Internet protocol security (ipsec) packet processing for multiple clients sharing a single network address
    linq.js的用法
    linq.js的用法
    程序员每天应该思考的5个问题,你有思考过吗?
    程序员每天应该思考的5个问题,你有思考过吗?
    程序员每天应该思考的5个问题,你有思考过吗?
    Win10 owerShell Get命令大全
    Win10 owerShell Get命令大全
    Win10 owerShell Get命令大全
  • 原文地址:https://www.cnblogs.com/lhat/p/6385590.html
Copyright © 2020-2023  润新知