js内置对象

 

内置对象

  • 指的是js自带的一些对象,如Math,Date,Array,String等

常用对象Math

Math是数学对象,不是构造函数,不需要new来调用直接,直接使用里面的方法和属性即可 函数中如有[]参数可放可不放

PI

cosole.log(Math.PI)

abs方法

作用输出绝对值,可把string转化为数字型

conole.log(Math.abs('-1'))//可转换为1
conole.log(Math.abs('大锤'))//不可转换·

floor

地板 向下取整 往小了取

console.log(Math.floor(1.1))//1
console.log(Math.floor(1.9))//1

ceil

天花板 向上取整 往大了取

console.log(Math.ceil(1.1))//2
console.log(Math.ceil(1.9))//2

round

  • 四舍五入
console.log(Math.round(1.1))//1
console.log(Math.round(1.5))//2
  • 特殊情况
console.log(Math.round(-1.5))//-1

因为在round中四舍五入需要往大了取

random

  1. Math.random随机返回一个浮点数,伪随机范围在(0,1)取0,取不到1
  2. 这个方法内不跟参数
  3. 代码
console.log(Math.random())
  1. 如想取得随机整数
function getRandom(min,max){
    return Math.floor(Math.random() * (max - min + 1))+min;
}

Date

Date对象是一个构造函数,必须使用new来,调用我们的日期对象

  1. 列表项如果里面没有参数,返回当前系统时间
var date = new Date;
console.log(date);
  1. 参数常用写法:数字型 2019, 10, 01 或者字符串 ‘2019-10-1’
  • 数字
var date = new Date(2019, 10, 01);
console.log(date);//输出结果19年11月1日,因为js一月是数字是0
  • 字符
var date = new Date( '2019-10-1');
console.log(date);//输出结果19年10月1日,此为常用写法

日期格式

  • 年月日
console.log(data.getFullYear())//返回当前年
console.log(date.Month())//返回 月份(0-11)
console.log(date.getDate())//返回 几号
console.log(date.Day)//返回周几 周一返回1 周日返回0
  • 时分秒
console.log(date.getHours())//时
console.log(date.getMinutes())//分
console.log(date.getSeconds())//秒

Date总的毫秒(时间戳)

Date获取总的毫秒,不是当前的毫秒,而是距离1970年1月1号总的毫秒

  1. 通过 valueOf() getTime()
var date = new Date();
console.log(date.valueOf();//现在距离1970.1.1总的毫秒数
console.log(getTime());
  1. 最常用写法
var date = +new Date();
console.log(date);
  1. h5新增写法 h5新增写法旧浏览器不支持
console.log(Date.now());
  1. 倒计时
  • 天 时 分 秒计算公式 d = parseInt(总秒数/ 60 / 60 / 24);//计算天数 h = parseInt(总秒数/ 60 / 60 % 24%);//计算小时 m = parseInt(总秒数/ 60 % 60 );//计算分钟 s = parseInt(总秒数 % 60);//计算当前秒数

js倒计时实例:

        function conutDown(time) {
           var nowTime = +new Date();//返回的是当前时间总的毫秒
           var inputTime = +new Date(time);//返回的是用户输入时间总的毫秒
           var times = (inputTime - nowTime) / 1000;//times是剩余时间总的毫秒
           var d = parseInt(times / 60 / 60 / 24);//天
           var h =parseInt(times / 60 / 60 %24 );//小时
           var m =  parseInt(times / 60 % 60 );//分
           var s = parseInt(times % 60);//秒
           return d + '' + h + '' + m + '' + s + '';
        }
        console.log(conutDown('2022-8- 18:30:00'));

Array

检查是否为数组

  1. nstanceof

是数组返回true,不是返回false 实例

 function reverse(arr) {
           if (arr instanceof Array ) {   //检查参数是否为数组
            var newArry = [];
            for(var i = arr.length - 1 ;i >= 0 ; i--) {
                newArry[newArry.length] = arr[i];

                }
             return newArry;
                } 
           else {
            return 'error 这个参数格式要求是数组格式[1,2,3]'
           }
        }
        console.log(reverse([1,2,3]));
  1. Array.isArray() h5 新增

数组添加元素方法

  1. push() 在我们的数组末尾 添加一个或者多个数组元素
  • push 可以给数组添加新元素
  • push() 参数直接写添加的元素
  • push完毕后,返回结果是新数组长度 代码示例:
    var arr = [1,2,3];
    arr.push(4, '大残');
    console.log(arr);//数组长度变为5
  1. unshift unshift() 在我们的数组开头 添加一个或者多个数组元素
  • unshift 可以给数组添加新元素
  • unshift () 参数直接写添加的元素
  • unshift 完毕后,返回结果是新数组长度
    var arr = [1,2,3];
    arr.unshift(0, '大残');
    console.log(arr);//数组长度变为5

删除数组

  1. pop
  • 删除数组最后一个元素
  • 没有参数
  • 返回值是删除的元素
  1. shift
  • shift删除数组第一个元素
  • 没有参数
  • 返回值是删除的元素

数组排序

  1. reverse 翻转数组 示例:
        var arr = [11, 2, 3];
        arr.reverse();
        console.log(arr);//输出结果为3,2,1
  1. sort
  • 数组排序默认为字典排序
var arr = [12,5,1,11,87,8];
        arr.sort();
        console.log(arr);//输出为1, 11, 12, 5, 8, 87
  • 升降序
        var arr = [12,5,1,11,87,8];
        arr.sort(function(a, b) {
            return a - b;//升序排列
            // return b - a;//降序排列
        });
        console.log(arr);

查询数组索引

  1. indexOf
  • indexOf(数组元素) 作用是返回数组元素索引号(从前往后)
  • 有相同的数组,只返回第一个的索引号
  • 如果早不到数组返回 -1
  1. lastIndexOf
  • lastIndexOf(数组元素)作用是返回数组元素索引号(从后往前)
  • 有相同的数组,只返回第一个的索引号
  • 如果早不到数组返回 -1

案例,数组重组

        function unique(arr) {
            var newArry = [];
            for(var i = 0; i < arr.length; i++) {
                if (newArry.indexOf(arr[i]) === -1) {
                    newArry.push(arr[i]);
                }
            }
            return newArry;
    }
    var demo = unique(['a', 'a', 'c', 'c', 'c', 'w', 'd']);
    console.log(demo);//输出为['a', 'c', 'w', 'd']

数组转换字符串

数组转换字符串

  1. toString
    var arr = [1, 2, 3];
    console.log(arr.toString());//1,2,3
    
  2. join(分隔符)
    var arr = ['g', 'b', 'p'];
    console.log(arr.join());//g,b,p 默认为逗号
    console.log(arr.join('&'));//g&b&p
    
  3. split(分隔符) 字符串 转化为数组

字符串对象

  1. charAt(索引号) 根据索引号返回字符
  2. charCodeAt(索引号) 返回相应索引号的字符的ascii码 判断用户按下的按键
  3. str[索引号] h5新增
  4. concat (‘字符串1’, ‘字符串2’…..)
  5. substr(‘截取起始位置’, ‘截取几个字符’)
  6. str.replace(‘被替换’, ‘替换’)