js内置对象(date对象和math对象)

一、Date对象

  Date对象用于处理日期和时间

 

  Date对象

  使用Date对象获取时分秒

  1、语法:

    Var 日期对象 = new Date(参数);

    通过new 关键词来定义 Date对象

    Date对象里面的参数  大多数都是可选的  在不指定的情况下, 默认是0

  2、常用的函数

      

    

    

二、Math对象

   Math对象用于执行数学任务

 

   Math对象是一个对象但不是一个函数

 

   Math对象属性和方法

  1、Math对象常用的属性和方法

    Math.floor():向下取整(下舍去) 
    Math.ceil():向上取整(上进入) 
    Math.round():四舍五入 
    Math.abs():取绝对值 
    Math.max():比较(取大) 
    Math.min():比较(取小) 
    Math.pow() : 取幂(两个参数第一个参数为底数,第二个为指数) 
    Math.sqrt() : 开平方 
    Math.random:0~1的一个随机数
    Math.cos():余弦值 
    Math.sin():正弦值 

 

 

  对常用随机数方法的扩展: 
    1、Math.round(Math.random())//0,1随机 
    2、Math.round(Math.random()*a) //0~a随机 
    3、Math.round(Math.random()*(a-b)+b)//b~a随机

三、封装对象

  1、封装一个对象,用来求多个数字的最大值,最小值

      function MyMath() {
            this.getMax=function(...res){
                let max=res[0];
                for(var x=0;x<res.length;x++){
                    if(max<res[x]){
                        max=res[x];
                    }
                }
                return max;
            }
            this.getMin=function(arr){
                let min=arguments[0];
                for(var x=0;x<arguments.length;x++){
                    if(min>arguments[x]){
                        min=arguments[x];
                    }
                }
                return min;
            }
            this.getA=function(arr){
                var min=arr[0]
            
                for(var x=0;x<arr.length;x++){
                    if(min>arr[x]){
                        min=arr[x];
                    }
                    
                }
                console.log("awef"+arr[min]);
                return min;
            }
        }
        
     var a=new MyMath();
     console.log( "最大值为"+ a.getMax(56,23,3,234,234,32,34,32,4,23,423,4,23,6));
     console.log("最小值为"+ a.getMin(56,23,3,234,234,32,34,32,4,23,423,4,23,6));
     var arr=[2,234,234,54,28,3,724,234,23,4]
     console.log("最小值为"+ a.getA(arr));
 

   

  结果为:
    
 
    其中 ...res(res可以是任意合法的名称)和arguments用法类似      ...res是ES6中的新语法,而arguments是ES5中的语法
posted @ 2021-01-22 20:58  小白小胖  阅读(183)  评论(0)    收藏  举报