js中的Math

静态对象: 不需要创建, 直接通过这个对象名字调用即可, (内置对象)
实例对象:  通过构造函数创建出来, 实例化的对象

    Math.PI --->π
     Math.abs(值)----> 取绝对值
    Math.ceil(值)   ---> 向上取整
    Math.floor(值)   ---> 向下取整
    Math.round(值)   ---> 四舍五入
    Math.max(值1, 值2....) 取一堆数字的最大值
    Math.min(值1, 值2....) 取一堆数字的最小值
    Math.pow(值1, 值2) 次方
    Math.sqrt(值1) 平方根
    Math.random() 随机数  区间是 0-1

    console.log(Math.ceil(4.1));//5
    console.log(Math.floor(4.9));//4
    console.log(Math.abs(-4.9));//4.9
    console.log(Math.round(4.5));//5
    
    console.log(Math.max(10,1, 50, 100));//100
    console.log(Math.min(10,1, 50, 100));//1
    
    console.log(Math.pow(2, 5));//次方
    console.log(Math.sqrt(16));//平方根

    取0-4的随机整数
    console.log(Math.floor(Math.random()*5));
    console.log(parseInt(Math.random()*5));

 //自定义构造函数
    function MyMath() {
        //添加方法
        this.getMax = function () {
            console.log(arguments);
            //假设一个最大值
            var max = arguments[0];//获取实参的个数, 以伪数组的形式展示
            for( var i = 0 ; i <arguments.length; i++){
                if( max < arguments[i]){
                    max = arguments[i];
                }
            }
            return max;
        }
    }
    //实例化对象
    var m = new MyMath();
   console.log(m.getMax(10, 20, 100, 40, 50));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值