Javascript内置对象

发布时间 2023-04-13 17:57:37作者: 摆烂员

内置对象(常用方法)

Math(不是构造函数,直接调用)

/**

  • @file 封装自己的myMath对象
  • @author lxs
    /
    var myMath = {
    PI: 3.1415926,
    /
    *
    • @description 求最小值
    • @returns {Number} max
    • @example max(3,5)//5
      /
      max: function () {
      var max = arguments[0];
      for (var i = 1; i < arguments.length; i++) {
      if (max < arguments[i]) {
      max = arguments[i];
      }
      }
      return max;
      },
      /
      *
    • @description 求最小值
    • @returns {Number} min 返回结果
    • @example min(10,2) //2
      */
      min: function () {
      var min = arguments[0];
      for (var i = 1; i < arguments.length; i++) {
      if (min > arguments[i]) {
      min = arguments[i];
      }
      }
      return min;
      },
      };

console.log(myMath.PI);
console.log(myMath.max());
console.log(myMath.min());
//求绝对值
console.log(Math.abs(1)); //1
console.log(Math.abs(-1)); //1
console.log(Math.abs("-1")); //隐式转换 会把字符串-1 转换为数字型
console.log(Math.abs("sss")); //Nan
//取整数
console.log(Math.floor(1.1)); //1 向下取整
console.log(Math.floor(1.9)); //1 向下取整

console.log(Math.ceil(1.1)); //2 向上取整
console.log(Math.ceil(1.9)); //2 向上取整

console.log(Math.round(1.1)); //1 四舍五入
console.log(Math.round(1.5)); //2 四舍五入
console.log(Math.round(1.9)); //2 四舍五入
console.log(Math.round(-1.1)); //-1 四舍五入
console.log(Math.round(-1.5)); //-1 四舍五入
//Math.random 随机数 [0-1) 前闭后开
//获取两个数之间的随机整数
/**

  • 获取两个数之间的随机整数
  • @param {Number} min
  • @param {Number} max
  • @returns a 随机整数
    /
    var getRandom = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1)) + min;
    };
    console.log(getRandom(1, 10));
    //随机点名
    var arr = ["tom", "jim", "zhangsan", "lisi"];
    console.log(arr[getRandom(0, arr.length - 1)]);
    /
    *
    *猜数字
    */
    var guess = () => {
    while (true) {
    let num = prompt("请输入您要猜的数字");
    let random = getRandom(1, 10);
    if (num > random) {
    alert("猜大了");
    } else if (num < random) {
    alert("猜小了");
    } else {
    console.log(random);
    alert("猜对了");
    break;
    }
    }
    };
    guess();

Date()对象

Date()日期对象 是一个构造函数,必须使用new来调用创建我们的日期对象
var arr = new Array();//创建一个数组对象
var obj = new Object();//创建一个对象实例

案例

///////////日期对象
//1.使用date 如果没有参数则返回系统当前的时间(标准时间)
var date = new Date();
console.log(date);//返回系统当前的时间
//参数常用的写法 数字型 2019,10,01 或者是字符串型(常用)'2019-10-01 08-08-08'
var date = new Date(2019,10,01 );
console.log(date);//返回2019/10/01
var date = new Date('2019-10-01 08:08:08');
console.log(date);//返回2019-10-01 08:08:08

//格式化日期 年月日
var date= new Date();
//获取年
console.log(date.getFullYear());//返回年
console.log(date.getMonth)+1;//返回月份会小一个月 需要加1
console.log(date.getDate);//返回 几号
console.log(date.getDay);//返回一周的第几天[0-6] 周一返回的是1 周日返回的是0,周六返回的是6
//格式化时间
/**
*

  • @param {Date} date
  • @return date(格式化日期)
  • @example dateformat(new Date(2019,10,01))//指定日期
  •      dateformat(new Date())//当前时间
    

/
var dateformat=(date)=>{
var year= date.getFullYear();
var mounth= date.getMonth()+1;
var day= date.getDate();
var weekCollection=['星期日','星期一','星期二','星期三','星期四','星期五','星期六']
var week= weekCollection[date.getDay()];
var times = date.getHours();//返回小时
var minutes =date.getMinutes();//返回分钟
var seconds =date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds();//返回秒数
date=year+'年'+mounth+'月'+day+'号'+week+' '+times+':'+minutes+':'+seconds;
return date;
}
console.log(dateformat(new Date()));
console.log(dateformat(new Date(2019,10,01)));
//获得Date总的毫秒数(时间戳),不是当前时间的毫秒数,而是距1970/1/1的毫秒数
var date = new Date();
console.log(date.valueOf());
console.log(date.getTime());
// 简单的写法(最常用的写法)
var date1 =+new Date();//+new Date() 返回总的毫秒数
console.log(date1);
/
*

  • 倒计时

  • @param {Date} date

  • return 剩余时间
    */
    var countdown = (time)=>{

    var inputTime =+new Date(time);//获取用户输入事件的时间戳
    var nowtime = +new Date();//获取当前的时间戳
    var times= (inputTime-nowtime)/1000;//毫秒数转换为秒数
    var day = parseInt(times/60/60/24); //计算天数
    day=day<10?'0'+day:day;
    var hours = parseInt(times/60/60%24); //计算小时数
    hours = hours<10?'0'+hours:hours
    var minutes = parseInt(times/60%60); //计算分钟数
    minutes = minutes<10?'0'+minutes:minutes
    var seconds = parseInt(times%60); //计算秒数
    seconds = seconds<10?'0'+seconds:seconds
    return day+'天'+hours+'时'+minutes+'分'+seconds+'秒';
    }
    console.log(countdown('2023/4/14'));