js中Math函数对象常用方法和es6函数新增方法

【箭头函数】
1.=>函数的简写方式;
2.左边是函数的参数,右边是函数的执行语句;
3.如果参数不是一个,那么参数要用()包裹;
4.如果执行语句不止一条要用{}包裹;
5.如果有返回值要用return,如果返回的是对象,要用()包裹;
6.箭头函数的this指向当前的执行环境;


如图.png

【函数参数】
1.默认参数:
function add(a=1,b=1){ alert(a+b); } add(); add(2,3);
2.不定参数:
function add2(...args){
var re = args.reduce((a,b)=>a+b); alert(re); } add2(1,2);
add2(1,2,3); add2(1,2,3,4);

[JS中Math函数的常用方法]

Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’
console.dir(Math) 查看Math的所有函数方法。
1,Math.abs() 获取绝对值

Math.abs(-12) = 12</pre>

2,Math.ceil() and Math.floor() 向上取整和向下取整

console.log(Math.ceil(12.03));//13
console.log(Math.ceil(12.92));//13
console.log(Math.floor(12.3));//12
console.log(Math.floor(12.9));//12</pre>

3,Math.round() 四舍五入
注意:正数时,包含5是向上取整,负数时包含5是向下取整。

Math.round(-16.3) = -16
Math.round(-16.5) = -16
Math.round(-16.51) = -17</pre>

4,Math.random() 取[0,1)的随机小数
案例1:获取[0,10]的随机整数

console.log(parseInt(Math.random()*10));//未包含10

console.log(parseInt(Math.random()*10+1));//包含10

案例2:获取[n,m]之间的随机整数

Math.round(Math.random()*(m-n)+n)</pre>

5,Math.max() and Max.min() 获取一组数据中的最大值和最小值

console.log(Math.max(10,1,9,100,200,45,78));
console.log(Math.min(10,1,9,100,200,45,78));</pre>

6,Math.PI 获取圆周率π 的值

console.log(Math.PI);</pre>

7,Math.pow() and Math.sqrt()

Math.pow()获取一个值的多少次幂
Math.sqrt()对数值开方

Math.pow(10,2) = 100;
Math.sqrt(100) = 10;</pre>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容