JavaScript Math对象详解

转载请注明预见才能遇见的博客:https://my.csdn.net/

原文地址:https://blog.csdn.net/weixin_42787326/article/details/81299435

JavaScript Math对象详解

目录

JavaScript Math对象详解

1.理解实例对象、静态对象、实例方法、静态方法

2.Math中常用的静态方法

Math.PI----π---==分割==Math.E----常数的底数==分割==Math.abs(值)-----绝对值==分割==Math.ceil(值)----向上取整==分割==Math.floor(值)---向下取整==分割==

Math.Max()---一组数字中的最大值==分割==Math.Min()---一组数字中的最小值==分割==Math.random---随机数字==分割==Math.sqrt()----开平方==分割==Math.pow()----一个数字的多少次幂 ==分割==

3.Math中Math.random() 看代码

练习:自己定义一个对象,实现系统的max的方法

练习:随机产生一个十六进制的颜色值 


Math 是一个对象,但是不是一个函数,Math对象下的属性和方法都是静态

1.理解实例对象、静态对象、实例方法、静态方法

实例对象:通过构造函数创建出来,实例化的对象

静态对象:不需要创建,直接就是一个对象,方法(静态方法)直接通过这个对象名字调用,

实例方法:必须通过实例对象调用

静态方法:必须通过大写的对象调用

2.Math中常用的静态方法

Math.PI----π---==分割==
Math.E----常数的底数==分割==
Math.abs(值)-----绝对值==分割==
Math.ceil(值)----向上取整==分割==
Math.floor(值)---向下取整==分割==

console.log(Math.PI);//3.141592653589793
console.log(Math.E);//2.718281828459045

console.log( Math.abs('-1'));//1
console.log( Math.abs(-2));//2
console.log(Math.abs(null));//---------0  重点
console.log(Math.abs("string"));//NaN 

console.log(Math.ceil(12.3));//13

console.log(Math.floor(12.3));//12


Math.Max()---一组数字中的最大值==分割==
Math.Min()---一组数字中的最小值==分割==

Math.random---随机数字==分割==
Math.sqrt()----开平方==分割==
Math.pow()----一个数字的多少次幂 ==分割==

console.log(Math.max(10,1,9,100,200,45,78));//200
console.log(Math.min(10,1,9,100,200,45,78));//1
console.log(Math.pow(2,4));//16
console.log(Math.sqrt(16));//4

//0-4  没有5
console.log(parseInt(Math.random()*5)+1);//4
console.log(parseInt(Math.random()*100)+1);//67

3.Math中Math.random() 看代码

//生成0-1的随机数  random是没有参数的
console.log(Math.random());

//生成0-5的随机数
console.log(Math.random()*5);
console.log(Math.random(1,5));

//生成0-10的随机数
console.log(Math.random()*10);

练习:自己定义一个对象,实现系统的max的方法

function MyMath() {
  //添加了一个方法
  this.getMax=function () {
    //所有数字中的最大值
    var max=arguments[0];
    for(var i=0;i<arguments.length;i++){
      if(max<arguments[i]){
        max=arguments[i];
      }
    }
    return max;
  };
}
//实例对象
var mt=new MyMath();
var result=mt.getMax(10,20,30,40,100,3);
console.log(result);

练习:随机产生一个十六进制的颜色值 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>$永远的24k纯帅$</title>
  <style>
    div {
      width: 300px;
      height: 200px;
      background-color: pink;
    }
  </style>
  <script>
    //随机产生一个十六进制的颜色值
    //封装成一个函数
    console.log(parseInt(Math.random() * 5));

    function getColor() {
      var str = "#";
      //一个十六进制的值的数组
      var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
      for (var i = 0; i < 6; i++) {
        //产生的每个随机数都是一个索引,根据索引找到数组中对应的值,拼接到一起
        var num = parseInt(Math.random() * 16);
        str += arr[num];
      }
      return str;
    }
    //页面记载的事件
    window.onload = function () {
      //在文档中通过id属性的值查找这个元素(标签).设置该标签的背景颜色
      document.getElementById("dv").style.backgroundColor = getColor();
    };
    //console.log(getColor());
  </script>
</head>
<body>
<div id="dv"></div>

</body>
</html>

JavaScript Math对象详解

博客地址:https://blog.csdn.net/weixin_42787326/article/details/81299435

猜你喜欢

转载自blog.csdn.net/weixin_42787326/article/details/81299435