Math称为数学函数,但是它属于对象类型的
typeof Math;//'object'
之所以叫数学函数,是因为Math这个对象中提供了很多操作数字的方法
console.dir(Math);//Math 是一个对象,有很多属性和方法
/*
控制台打印:
Math={
//属性
E: 2.718281828459045
LN10: 2.302585092994046
LN2: 0.6931471805599453
LOG10E: 0.4342944819032518
LOG2E: 1.4426950408889634
PI: 3.141592653589793
SQRT1_2: 0.7071067811865476
SQRT2: 1.4142135623730951
Symbol(Symbol.toStringTag): "Math"
__proto__: Object
//方法
abs: ƒ abs()
acos: ƒ acos()
acosh: ƒ acosh()
asin: ƒ asin()
asinh: ƒ asinh()
atan: ƒ atan()
atanh: ƒ atanh()
atan2: ƒ atan2()
ceil: ƒ ceil()
cbrt: ƒ cbrt()
expm1: ƒ expm1()
clz32: ƒ clz32()
cos: ƒ cos()
cosh: ƒ cosh()
exp: ƒ exp()
floor: ƒ floor()
fround: ƒ fround()
hypot: ƒ hypot()
imul: ƒ imul()
log: ƒ log()
log1p: ƒ log1p()
log2: ƒ log2()
log10: ƒ log10()
max: ƒ max()
min: ƒ min()
pow: ƒ pow()
random: ƒ random()
round: ƒ round()
sign: ƒ sign()
sin: ƒ sin()
sinh: ƒ sinh()
sqrt: ƒ sqrt()
tan: ƒ tan()
tanh: ƒ tanh()
trunc: ƒ trunc()
}
*/
一、Math
数学函数:但是它不是一个函数,它是一个对象,对象中存储了很多操作数字的属性方法,因此被称为数学函数
Math={
PI: 3.141592653589793,
abs:function(){[native code]},
ceil:function(){[native code]}
}
调用数学方法:
Math.abs();//调用方法
Math.PI;//调取属性
通过console.dir(Math);可以获取Math的所有方法,百度“MDN”可以查所有方法的用法
以上几个方法,包括isNaN()方法,传递的n不是数字类型的值:先基于Number()转换为数字再处理,Number()=>0,其他的都是不传值时为NaN,传递空字符串时都为0.
console.log(Math.abs(-12.5));
console.log(Math.abs(0));
console.log(Math.abs('-11'));
console.log(Math.abs(true));
console.log(Math.abs(null));
console.log(Math.abs(undefined));
console.log(Math.abs());
console.log(Math.abs(''));
console.log(Math.abs('aa'));
/*
12.5
0
11
1
0
NaN
NaN
0
NaN
*/
Number()
console.log(Number());
console.log(Number(''));
console.log(Number(undefined));
console.log(Number([12]));
console.log(Number({}))
console.log(Number([12,34]))
/*
0
0
NaN
12
NaN
NaN
*/
isNaN()
console.log(isNaN());
console.log(isNaN(''));
console.log(isNaN(undefined));
console.log(isNaN(null));
console.log(isNaN([1]));
/*
true
false
true
false
false
*/
console.log(Math.ceil(12));
console.log(Math.ceil(12.1));
console.log(Math.ceil(12.56));
console.log(Math.ceil(-12.2));
console.log(Math.ceil(-12.9));
console.log(Math.ceil('11.3'));
console.log(Math.ceil(''));
console.log(Math.ceil());
console.log(Math.ceil('12.5px'));
console.log(Math.ceil(null));
console.log(Math.ceil(undefined));
console.log(Math.ceil(true));
/*
12
13
13
-12
-12
12
0
NaN
NaN
0
NaN
1
*/
console.log(Math.floor(12));
console.log(Math.floor(12.1));
console.log(Math.floor(12.56));
console.log(Math.floor(-12.2));
console.log(Math.floor(-12.9));
console.log(Math.floor('11.3'));
console.log(Math.floor(''));
console.log(Math.floor());
console.log(Math.floor([]));
console.log(Math.floor('12.5px'));
console.log(Math.floor(null));
console.log(Math.floor(undefined));
console.log(Math.floor(true));
/*
12
12
12
-13
-13
11
0
NaN
0
NaN
0
NaN
1
*/
console.log(Math.round(12));
console.log(Math.round(12.1));
console.log('12.5',Math.round(12.5));//13,正数里0.5是0
console.log(Math.round(12.56));
console.log(Math.round(-12.2));
console.log('-12.5:',Math.round(-12.5));//-12,负数中-0.5是0
console.log(Math.round(-12.9));
console.log(Math.round('11.3'));
console.log(Math.round(''));
console.log(Math.round());
console.log(Math.round([]));
console.log(Math.round('12.5px'));
console.log(Math.round(null));
console.log(Math.round(undefined));
console.log(Math.round(true));
console.log(Math.round(0.5));
console.log(Math.round(-0.5));
/*
12
12
12.5 13
13
-12
-12.5: -12
-13
11
0
NaN
0
NaN
0
NaN
1
1
-0
*/
console.log(Math.max(1, 3, 5, '12'));
console.log(Math.min(3, true, 5, '12'));
console.log(Math.min(Math.max([1, 2, 3, 4])));
/*
12
1
NaN
*/
console.log(Math.sqrt(9));
console.log(Math.sqrt(-9));//NaN
console.log(Math.pow(2, 10));//1024 2的10次幂是1024,必须记住 在程序中1024才是整数
/*
3
NaN
1024
*/
console.log(Math.random().toFixed(2));
console.log(Math.random().toFixed(2));
for (let i = 0; i < 5; i++) {
console.log(Math.random());
}
eg:获取1~10之间的随机整数,包括1和10;
for (let i = 0; i < 10; i++) {
let n = Math.random().toFixed(1) * 10;
console.log(Math.ceil(n));
}
扩展题:获取[n~m]之间的随机整数,包含n和m,n
//25~30之间的随机整数
for(let i=0;i<=10;i++){
console.log(Math.round(Math.random()*(30-25)+25));
}
eg:获取1~10之间的随机整数,包括0和10;
Math.round(Math.random()*(10-1)+1
Math.random();
//0-1 *9 => 0-9 +1 =>1-10
思考题:如何基于Math.min/max获取数组中的最大最小值?
Math.max([1,2,3,4]);//=>NaN 此处是只传第一个值,是个数组,和内置的语法要求不符合
//一开始的解决方法,利用...展开运算符
console.log(Math.max(...[1,2,3]));