说到JavaScript对象首先需要说一下内置对象
1.内置对象
内置对象:就是js语言自带的一些对象,这些对象供开发者使用,并提供了一些常用的或是最基本而必要的功能(属性或者方法)。
内置对象的优点:就是帮助开发者更快的进行开发。
2.数学对象(Math对象)
Math对象:不是一个构造函数,所以我们不需要new来调用,而是直接使用里面的属性和方法即可。
console.log(Math.PI);
// Math.PI是一个属性,是可以直接进行调用,然后打印输出的
console.log(Math.max(1, 99, 'pink'));
// 当里面有一个非数字的,输出的时候就是NaN.
console.log(Math.max(1,99,46,53)) //这里输出的就是99
console.log(Math.max())
//Math.max()里面没有输入数值(里面的数值是可以写也是可以不写的),返回的就是-Infinity
案例一:封装一个数学对象,里面有PI,最大值和最小值
// 封装自己的数学对象,里面有PI,最大值,最小值
// 这里是利用字面量创建对象,所以每个对象的结尾需要用逗号进行结尾
var myMath = {
PI: 3.141592653,
max: function() {
var max = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
},
min: function() {
var min = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (arguments[i] < min) {
min = arguments[i];
}
}
return min;
},
}
console.log(myMath.PI);
console.log(myMath.max(1,5,4,7,9,22));
console.log(myMath.min(1,5,4,7,9,22));
1)绝对值方法
console.log(Math.abs(1)); //输出的结果为1
console.log(Math.abs(-1)); //输出的结果为1
console.log(Math.abs('-1'));
//输出的结果为1 ,这里进行了隐式转换,会把字符串的-1 转换为数值型的1
console.log(Math.abs('pink')); //输出的结果为NaN
// 因为这里无法将字符串的值,转换为数值型的,所以输出的结果就为NaN.
2)三个取整的方法
1)Math.floor() 向下取整,往最小的进行取整操作
console.log(Math.floor(1.1)); //输出结果为1
console.log(Math.floor(1.9)); //输出结果也为1
console.log(Math.floor(-1.1)); //输出结果也为-2
console.log(Math.floor(-1.9)); //输出结果也为-2
2)Math.ceil() 向上取整 往最大的进行取整操作
console.log(Math.ceil(1.1)); //输出的结果为2
console.log(Math.ceil(1.9)); //输出的结果也为2
console.log(Math.ceil(-1.1)); //输出的结果也为-1
console.log(Math.ceil(-1.9)); //输出的结果也为-1
3)Math.round() 四舍五入,就近进行取整操作 ,但是-3.5,取整为-3( .5进行取整的时候,他是往大的方向进行取整)
console.log(Math.round(3.4)); //输出的结果为3
console.log(Math.round(3.6)); //输出的结果为4
console.log(Math.round(-3.4)); //输出的结果为-3
console.log(Math.round(-3.5)); //输出的结果为-3
console.log(Math.round(-3.6)); //输出的结果为-4
3)随机数方法randon() 返回一个随机小数 [0,1)
console.log(Math.random()); //产生的随机小数取值范围 0<=x<1
4)想要得到两个数之间的随机整数
Math.floor(Math.random() * (max - min + 1)) + min;
//该算式含最大值也含最小值
Math.floor(Math.random() * (max - min)) + min;
//该算式含最小值不含最大值
我们在获取随机整数主要运用的就是上面这两种方法
function getRandom(min , max){
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandom(1,10))
随机整数的案例:随机点名,随机在'关羽','张飞','马超','赵云','黄忠','刘备','诸葛亮','姜维','pink'中进行点名。
var arr=['关羽','张飞','马超','赵云','黄忠','刘备','诸葛亮','姜维','pink'];
function getRandom(min , max){
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值也含最小值
}
console.log(arr[getRandom(0,arr.length-1)]);
总结性案例:Math对象案例:猜数字游戏,随机生成一个数字,并让用户输入一个数字。与随机生成的数字进行比对,判断用户输入的数字是否匹配成功,并且给用户10次机会,机会用完,用户也无法进行猜测。
function getRandom(min ,max){
return Math.floor(Math.random()*(max-min+1))+min
}
var cishu1=1;
var cishu2=10;
var start=1,end=100;
var random=getRandom(start,end);
while(true){ //这里是一个死循环
var num=prompt('请用户输入你要猜测的数字为:');
if(num > random){
alert("你的数字猜大了,他应该小于" + num);
}else if(num < random){
alert("你的数字猜小了,他应该大于" + num);
}else{
alert('Bingo!你真棒只猜了' + cishu1 + '次就猜对咯!');
break;
}
if(cishu1 == cishu2){
alert('您猜的次数已经用完了,很可惜,你没有机会了')
break;
}
cishu1++;
}
本文含有隐藏内容,请 开通VIP 后查看