欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS內(nèi)置對象和Math對象知識點(diǎn)詳解

 更新時間:2020年04月03日 08:44:39   作者:About  
在本篇文章里小編給大家分享的是關(guān)于JS內(nèi)置對象和Math對象知識點(diǎn)詳解內(nèi)容,有需要的朋友們可以參考下。

Math對象

<script>
    // Math數(shù)學(xué)對象 不是一個構(gòu)造函數(shù) ,所以我們不需要new 來調(diào)用 而是直接使用里面的屬性和方法即可
    console.log(Math.PI); // 一個屬性 圓周率
    console.log(Math.max(1, 99, 3)); // 99
    console.log(Math.max(-1, -10)); // -1
    console.log(Math.max(1, 99, 'pink老師')); // NaN
    console.log(Math.max()); // -Infinity
  </script>

自己封裝對象

<script>
    // 利用對象封裝自己的數(shù)學(xué)對象 里面有 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, 9));
    console.log(myMath.min(1, 5, 9));
  </script>

一些常用的方法

<script>
    // 1.絕對值方法
    console.log(Math.abs(1)); // 1
    console.log(Math.abs(-1)); // 1
    console.log(Math.abs('-1')); // 隱式轉(zhuǎn)換 會把字符串型 -1 轉(zhuǎn)換為數(shù)字型
    console.log(Math.abs('pink')); // NaN 

    // 2.三個取整方法
    // (1) Math.floor()  地板 向下取整 往最小了取值
    console.log(Math.floor(1.1)); // 1
    console.log(Math.floor(1.9)); // 1
    // (2) Math.ceil()  ceil 天花板 向上取整 往最大了取值
    console.log(Math.ceil(1.1)); // 2
    console.log(Math.ceil(1.9)); // 2
    // (3) Math.round()  四舍五入 其他數(shù)字都是四舍五入,但是 .5 特殊 它往大了取 
    console.log(Math.round(1.1)); // 1
    console.log(Math.round(1.5)); // 2
    console.log(Math.round(1.9)); // 2
    console.log(Math.round(-1.1)); // -1
    console.log(Math.round(-1.5)); // 這個結(jié)果是 -1
  </script>
<script>
    // 1.Math對象隨機(jī)數(shù)方法  random() 返回一個隨機(jī)的小數(shù) 0 =< x < 1
    // 2. 這個方法里面不跟參數(shù)
    // 3. 代碼驗(yàn)證 
    console.log(Math.random());
    // 4. 我們想要得到兩個數(shù)之間的隨機(jī)整數(shù) 并且 包含這2個整數(shù)
    // Math.floor(Math.random() * (max - min + 1)) + min;
    function getRandom(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    console.log(getRandom(1, 10));
    // 5. 隨機(jī)點(diǎn)名 
    var arr = ['張三', '張三豐', '張三瘋子', '李四', '李思思', 'pink老師'];
    // console.log(arr[0]);
    console.log(arr[getRandom(0, arr.length - 1)]);
  </script>

到此這篇關(guān)于JS內(nèi)置對象和Math對象知識點(diǎn)詳解的文章就介紹到這了,更多相關(guān)JS內(nèi)置對象和Math對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論