Js数学函数-Math方法、Math属性

Ⅰ- 壹 - Math简介

  • Math全部都是静态属性和方法 Math是一种静态类
  • 不需要创建对象
  • 该类别不能实例化 var met=new Math()//错误
  • Math 是数学函数,但又属于对象数据类型 typeof Math => ‘object’

Ⅱ - 贰 -Math属性

Math属性名介绍
Math.E返回算术常量 e,即自然对数的底数(约等于2.718)。
Math.LN2返回 2 的自然对数(约等于0.693)。
Math. LN10返回 10 的自然对数(约等于2.302)。
Math.LOG2E返回以 2 为底的 e 的对数(约等于 1.414)。
Math.LOG10E返回以 10 为底的 e 的对数(约等于0.434)。
Math.PI返回圆周率(约等于3.14159)。
Math.SQRT1_2返回返回 2 的平方根的倒数(约等于 0.707)。
Math.SQRT2返回 2 的平方根(约等于 1.414)。
  • Math的属性用法
console.log(Math.PI);//3.141592653589793
console.log(Math.SQRT2);//1.4142135623730951

Ⅲ - 叁 -Math方法

一 Math方法总汇

Math方法描述
Math.abs(x)返回数的绝对值。
Math.acos(x)返回数的反余弦值。
Math.asin(x)返回数的反正弦值。
Math.atan(x)以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。
Math.atan2(y,x)返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。
Math.ceil(x)对数进行上舍入。
Math.cos(x)返回数的余弦。
Math.exp(x)返回 e 的指数。
Math.floor(x)对数进行下舍入。
Math.log(x)返回数的自然对数(底为e)。
Math.max(x,y)返回 x 和 y 中的最高值。
Math.min(x,y)返回 x 和 y 中的最低值。
Math.pow(x,y)返回 x 的 y 次幂。
Math.random()返回 0 ~ 1 之间的随机数。
Math.round(x)把数四舍五入为最接近的整数。
Math.sin(x)返回数的正弦。
Math.sqrt(x)返回数的平方根。
Math.tan(x)返回角的正切。

二 常用Math方法

0 随机数方法 Math.random()

该方法用于生成一个随机数0~1之间的小数 包含0不包含1 (0=<x<1),方法没有参数

  • 生成随机数的封装
// Math . floor (Math. random() * (max -min + 1)) + min;
function getRandom(min, max) {
return Math. floor (Math. random() * (max-min + 1)) + min;
}
//生成1-10的随机数
console . log(getRandom(1, 10));
1 绝对值方法

Math.abs(x) 返回数的绝对值。

console .1og(Math. abs(1)); // 1
console . log(Math.abs(-1)); // 1 
console. log(Math.abs('-1')); //隐式转换会把字符串型-1转换为数字型
console. log(Math.abs('pink')); // NaN 
2 三个取整方法
(1) Math. floor() 向下取整往最小了取值
console .1og(Math. floor(1.1)); // 1
console .1og(Math. floor(1.9)); // 1
(2) Math.ceil() 向上取整 往最大了取值
console . log(Math.cei1(1.1)); // 2
console.1og(Math.ceil(1.9)); // 2 I
(3) Math. round() 四舍五入 负数往大了取
console. log (Math. round(1.1)); // 1
console . log(Math. round(1.5)); // 2
console.1og(Math.round(1.9)); // 2

console. log (Math. round( -1.1)); // -1
console. log(Math.round( -1.5)); //这个结果是-1

在这里插入图片描述

0 - 0 - 知识点:

静态方法

上面说 Math 全部都是静态属性和方法,下面来回顾一下什么叫静态方法:
静态方法也叫类的方法,是指把构造函数看成对象,增加的方法。例如数组中的 Array.from ( )、Array.isArray ( ) 。
除此之外还有实例化方法,例如 forEach()、map()。

var arr=new Array(1,2,3);
//或者 var arr=[1,2,3]
arr.forEach(function(){

})

简单来记,静态方法都是 类型.方法名(Array.from())

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值