内置对象 就是指 js 语言自带的一些对象,这些对象供开发者使用,并提供了一些常用的或是最基本而非必要的功能(属性和方法),内置对象最大的优点就是帮助我们快速开发。
Math 对象
如下所示,输出结果
console.log(Math.PI); // 3.1415926...
console.log(Math.floor(5.6)); // 5
console.log(Math.ceil(5.6)); // 6
console.log(Math.round(5.5)); // 6
console.log(Math.round(-5.5)); // -5
console.log(Math.abs(-5)); // 5
console.log(Math.max(1,2,3)); // 3
console.log(Math.min(1,2,3)); // 1
console.log(Math.random()); // 0 ~ 1 随机小数
获取指定范围的
// 以下函数返回 min(包含)~ max(不包含)之间的数字
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
// 以下函数返回 min(包含)~ max(包含)之间的数字 function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
随机整数