JavaScript (内置对象及方法)

JavaScript中的对象分为3种:内置对象、浏览器对象、自定义对象

JavaScript 提供多个内置对象:Math/Array/Number/String/Boolean...

对象只是带有属性方法的特殊数据类型。

内置对象的方法很多,我们只需要知道内置对象提供的常用方法,使用的时候查询文档。

一、Math对象

Math对象不是构造函数,它具有数学常数和函数的属性和方法,都是以静态成员的方式提供跟数学相关的运算来找Math中的成员(求绝对值,取整)

Math.PI                      // 圆周率     console.log(Math.PI);
Math.E                       // 常数的底数  console.log(Math.E);

Math.random()                // 生成随机数  返回一个浮点,  伪随机数在范围[0,1)
Math.floor()/Math.ceil()     // 向下取整/向上取整    console.log(Math.ceil(12.09));
Math.round()                 // 取整,四舍五入       console.log(Math.round(12.09));
Math.fround()                // 返回数字的最接近的单精度浮点型表示。  console.log(Math.fround(12.09));
Math.abs()                   // 绝对值         console.log( Math.abs(-2));//2
Math.max()/Math.min()        // 求最大和最小值   console.log(Math.max(10,1,9,100,200,45,78));

Math.sin()/Math.cos()        // 正弦/余弦  Math.sin(Math.PI / 2); // 1
Math.power()/Math.sqrt()     // 求指数次幂/求平方根console.log(Math.sqrt(4,2));

1.1 Math.random()

函数返回一个浮点,  伪随机数在范围[0,1),也就是说,从0(包括0)往上,但是不包括1(排除1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。

1.1.1 得到一个大于等于0,小于1之间的随机数

function getRandom() {
  return Math.random();
}

1.1.2 得到一个两数之间的随机数

这个例子返回了一个在指定值之间的随机整数。这个值比min大(如果min不是整数,可能相等)

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}

也许很容易让人想用舍入来完成这个任务,但是这样做会导致你的随机数处于一个不均匀的分布,这可能不符合你的需求

1.1.3 得到一个两数之间的随机整数,包括两个数在内

getRandomInt() 函数在最小值之上,它将排除最大值. 如果你需要结果包含最小值和最大值,怎么办呢?  getRandomIntInclusive() 函数将能完成.

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive 
}

1.2 综合实例

//随机产生一个十六进制的颜色值
//封装成一个函数
console.log(parseInt(Math.random() * 5));

function getColor() {
  var str = "#";
  //一个十六进制的值的数组
  var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
  for (var i = 0; i < 6; i++) {
    //产生的每个随机数都是一个索引,根据索引找到数组中对应的值,拼接到一起
    var num = parseInt(Math.random() * 16);
    str += arr[num];
  }
  return str;
}

二、Date对象

var dt = new Date();
//当前的时间---当前的服务器
console.log(dt);              //Wed Jun 13 2018 12:57:17 GMT+0800 (China Standard Time)

var dt = new Date("2017-08-12");
//传入的时间
console.log(dt);              //Sat Aug 12 2017 08:00:00 GMT+0800 (China Standard Time)


var dt = new Date("2017/08/12");
// 传入的时间
console.log(dt);              //Sat Aug 12 2017 00:00:00 GMT+0800 (China Standard Time)
// 获取时间的对象
var dt = Date.now();
console.log(dt);//毫秒        //1528865837155
总结:
var
dt=new Date(); console.log(dt); // var dt=+new Date(); //一种特殊的写法,只适用于这个Date的对象 // console.log(dt); console.log(dt.getFullYear()); // console.log(dt.getMonth()); //月---从0开始 console.log(dt.getDate()); // console.log(dt.getHours()); //小时 console.log(dt.getMinutes()); //分钟 console.log(dt.getSeconds()); // console.log(dt.getDay()); //星期---从0开始 console.log(dt.toDateString()); //日期 console.log(dt.toLocaleDateString()); //日期 console.log(dt.toTimeString()); //时间 console.log(dt.toLocaleTimeString(); //时间 console.log(dt.valueOf()); //毫秒
console.log(dt.toString()); // 转成字符串 Wed Jun 13 2018 13:08:36 GMT+0800 (China Standard Time)
 

格式化日期和时间

console.log(getDate(new Date()));

猜你喜欢

转载自www.cnblogs.com/dongye95/p/9177309.html