2023-07-15

arguments对象

arguments对象(不定型参数)
arguments对象:在函数体内具有数组功能的一个对象。但不是数组对象的实例(即不是数组类型)。
            arguments对象作用:保存函数实参。
            1、函数声明后,函数体内就存在了arguments对象。
            2、函数调用,初始化实参时,实参按照顺序保存在arguments对象中。
            3、通过下标值可以访问arguments对象中保存的实参值。
            4、arguments对象只能在函数中使用。
 function hello(){
         console.log(arguments[0],arguments[1],arguments[2]);
        }
hello(10,20,30,40)

随便输入几个数字计算和;
            //定义一个变量为he,初始值是0,用于储存和
            var he=0;
            function jisuan(num){
                for (var i=0;i<arguments.length;i++) {
                    
                    he+=arguments[i]
                }
                alert(he);
            }
            //调用函数计算并传入10,20,30,40
            jisuan(10,20,30,40)

Math 对象

Math 对象 :数学对象,提供对数据的数学计算

Math 对象无构造函数,无法被初始化,只提供静态属性和方法。

  1. 静态属性

    • Math.E : 常量e。 返回自然对数的底数:2.718281828459045
    • Math.PI : 常量π。返回圆周率的值 :3.141592653589793
    • Math.LN2 返回 2 的自然对数(约等于0.693)。
    • Math.LN10 返回 10 的自然对数(约等于2.302)。
    • Math.LOG2E 返回以 2 为底的 e 的对数(约等于 1.414)。
    • Math.LOG10E 返回以 10 为底的 e 的对数(约等于0.434)。
    • Math.SQRT1_2 返回返回 2 的平方根的倒数(约等于 0.707)。
    • Math.SQRT2 返回 2 的平方根(约等于 1.414)
  2. 静态方法(不常用)

    • Math.sin(value) :正弦函数
    • Math.cos(value) :余弦函数
    • Math.tan(value) :正切函数
    • Math.asin(value) :反正弦函数
    • Math.acos(value) :反余弦函数
    • Math.atan(value) :反正切函数
  3. 静态方法(常用)

    • Math.abs(value) :返回绝对值

      Math.abs('123'); // => 123 :纯数字字符串
      Math.abs('-123'); // => 123
      Math.abs(123); // => 123
      Math.abs(-123); // => 123
      Math.abs('123a'); // => NaN :非纯数字字符串
      
    • Math.ceil(value) : 对一个数向上取整,并不是四舍五入

      Math.ceil(2.7); // => 3
      Math.ceil(2.3); // => 3 :2.3 向上取整返回 3
      Math.ceil(-2.7); // => -2
      Math.ceil(-2.3); // => -2
      Math.ceil('2.7'); // => 3 :纯数字字符串
      Math.ceil('2.7a'); // => NaN :非纯数字字符串
      
    • Math.floor(value) :对一个数向下取整,并不是四舍五入

      Math.floor(2.7); // => 2
      Math.floor(2.3); // => 2
      Math.floor(-2.7); // => -3 :-2.7 向下取整返回 -3
      Math.floor(-2.3); // => -3
      Math.floor('2.7'); // => 2 :纯数字字符串
      Math.floor('2.7a'); // => NaN :非纯数字字符串
      
    • Math.round(value) : 四舍五入后取整

      Math.round(2.5); // => 3
      Math.round(2.4); // => 2
      Math.round(-2.6); // => -3
      Math.round(-2.5); // => -2 :-2.5四舍五入为 -2
      Math.round(-2.4); // => -2
      Math.round('2.7'); // => 3 :纯数字字符串
      Math.round('2.7a'); // => NaN :非纯数字字符串
      
    • Math.max(value1,value2...valueN) :返回参数中最大的值

      Math.max(1, 2, 3, 4, 5); // => 5
      Math.max(1, 2, 3, 4, '5' ); // => 5
      Math.max(1, 2, 3, 4, 'a'); // => NaN 
      
    • Math.min(value1,value2...valueN) :返回参数中最小的值

      Math.min(1, 2, 3, 4, 5); // => 1
      Math.min('1', 2, 3, 4, 5); // => 1
      Math.min(1, 2, 3, 4, 'a'); // => NaN 
      
    • Math.pow(x,y) :返回x的y次方

      Math.pow(2, 3); // => 8 :2的3次方
      Math.pow(3, 2); // => 9 :3的2次方
      Math.pow('4', 2); // => 16 :4的2次方
      Math.pow('2a', 2); // => NaN
      
    • Math.sqrt(value) :返回参数的平方根

      console.log( Math.sqrt(9) ); // => 3
      console.log( Math.sqrt(16) ); // => 4
      console.log( Math.sqrt('25') ); // => 5
      console.log( Math.sqrt('a') ); // => NaN
      
    • Math.random() :返回一个伪随机数,大于0,小于1.0

      Math.random(); // => 0.8982374747283757
      Math.random(); // => 0.39617531932890415
      
  4. Math常用方法统计

    1. Math.max()找最大
    2. Math.min()找最小
    3. Math.ceil()向上取整
    4. Math.floor()向下取整
    5. Math.round()四舍五入
    6. Math.random()0-1之间包含0的随机数
    7. Math.sqrt()算术平方根
    8. Math.abs()绝对值
    9. Math.PI() π值
    10. Math.floor(Math.random()*(大-小+1)+小) 随机数
    
    如果要一个 1~20的随机数使用:
    console.log(parseInt(Math.random() * 20) + 1);
    //最小是 0 * 20 = 0
    //最大是 0.9999 * 20 = 19.xxxxx; parseInt 以后是 19 
    //最后得+1
    要一个  15  ~ 33 之间的随机数
    原理很简单,最小的是多少,最大的是多少。
    最小是15,如果随机到0,是不是的是 15 ,   0+15
    最大是33,如果随机到 0.99999 * (最大的数 - 15 + 1)  + 15
    

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

推荐阅读更多精彩内容

  • avascript 数值简介:JavaScript 采用“IEEE 754 标准定义的双精度64位格式表示数字。 ...
    小棋子js阅读 3,621评论 0 0
  • 1. 摘要 本文讲解了Python语法的要点,便于入门者学习之用。 2.内容 2.1 基本语法 2.2.1 Pyt...
    笔名辉哥阅读 5,303评论 0 1
  •   引用类型的值(对象)是引用类型的一个实例。   在 ECMAscript 中,引用类型是一种数据结构,用于将数...
    霜天晓阅读 4,785评论 0 1
  • 这周准备了四级,但是更重要的是,那个毛概要补考了,一会儿晚上考试,水一篇,这周的东西都总结完了。 符 比如: 3<...
    Hello_word_李阳阅读 3,848评论 4 9
  • 1,javascript 基础知识 Array对象 Array对象属性 Arrray对象方法 Date对象 Dat...
    Yuann阅读 4,594评论 0 1