Search
JavaScript内置对象
Last updated
Unknown
- JS中的对象分为三种:自定义对象、内置对象、浏览器对象。
 
# Math 数学对象
Math不是一个构造函数,不需要用new来调用,而是直接使用里面的属性。具有数学常数和函数的属性和方法,跟数学有关的运算可以使用Math里的成员。
- 常用的
MathMath.PI 圆周率Math.floor() 向下取整Math.ceil() 向上取整Math.round() 四舍五入版就近取正,但-3.5会取值-3Math.abs() 绝对值Math.max() / Math.min() 求最大和最小值
 
# 得到一个两数之间的随机整数,包括两个数在内
1
2
3
4
5
  | function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}
  | 
# 猜数字游戏
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
  | function getRandom(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //含最大
}
var random = getRandom(1, 10);
while (true) {
	var num = prompt('猜1-10之间的一个数字')
	if (num > random) {
	alert('猜大了'); 
	} else if (num < random) {
	alert('猜小了')
	} else {
	alert('猜对了');
	break; //退出整个循环
	}
}
// 限制猜的次数,可以用for循环
  | 
# 随机点名
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  |         function getRandom(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min
        }
        // 声明一个数组
        let arr = ['tim', 'rob', 'bob', 'allen', 'emily', 'joy', 'muse', 'casio', 'may']
            // 生成一个随机数,作为数组索引号
        let random = getRandom(0, arr.length - 1);
        console.log(random)
        document.write(arr[random])
            // 为了不重复,删除名字
            // arr.splice(从哪里开始删,删几个)
        arr.splice(random, 1)
        console.log(arr)
  |