javascript高级程序设计--单体内置对象

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/codesWay/article/details/79987784
global:单体内置对象;

encodeURL(url):对url进行编码,事实上只会对url中的空格进行编码(%20),其他的都不会变,与之对应的是decodeURL(),换句话说只能反解析%20;

encodeURLComponent(url):也是对url进行编码,与encodeURL(url)的区别是:encodeURL(url)只会对url中的空格进行编码,而后者是对url中所有的非字母数字字符(包括空格)进行编码,与之对应的是decodeURLComponent(),所有的都能反编码;如:

 var url = "http://www.baidu.com  ";
         console.log(encodeURI(url));
         console.log(encodeURIComponent(url))
         var urlCoded = "http://www.baidu.com%20";
         console.log(decodeURI(urlCoded));
         console.log(decodeURIComponent(urlCoded))
Math对象中的一些方法:
            Math.min(num1,num2...)与Math.max(num1,num2...)
:找到一组数据中最小或者最大的那个数,并返回之;
             Math.ceil(num):向上舍入,也就是说对于小数,总是将其向上舍入为比其大且最接近的整数;
            Math.floor(num):向下舍入,也就是说对于小数,总是将其向下舍入为比其小且最接近的整数;
            Math.round(num):四舍五入,根据情况舍入;
            Math.random():产生一个0-1的随机数; Math.random()*(max - min)+min:产生一个介于min和max之间的随机数;
            Math.floor(Math.random*(max-min+1)+min):产生介于min-max之间的随机整数,为什么这么说呢,这是因为Math.random*(max-min+1)+min插产生的数介于min--max+1(不包括)之间的随机数,然后对其向下舍入,不就是在min和max之间了嘛
 //Math.min()与Math.max();
        console.log(Math.max(10, 23, 12, 34, 21, 08, 90));
        console.log(Math.min(10, 23, 12, 34, 21, 08, 90))
        //求一个数组中最大或者最小的值:Math.max.apply(Math,numArr)和Math.min.apply(Math,numArr)
        var numArr = [12,32,09,65,52,34,45];
        console.log(Math.max.apply(Math,numArr))//65,求数组中的最大值
        /*
        Math.ceil(),Math.floor(),Math.round();
         */
        //  Math.ceil()
        console.log(Math.ceil(-21.98));//-21
        console.log(Math.ceil(21.98));//22
        console.log(Math.ceil(21.01));//22
        // Math.floor();
        console.log(Math.floor(-21.98));//-22
        console.log(Math.floor(21.98));//21
        console.log(Math.floor(21.01));//21
        // Math.round()
        console.log(Math.round(-21.98));//-22
        console.log(Math.round(-21.34));//-21
        console.log(Math.round(21.01));//21;
        // Math.random()
        function getRandomNum(min,max){
            var scaleLen = max-min+1;
            return Math.floor(Math.random()*scaleLen+min)
        }
        alert(getRandomNum(11,23))


猜你喜欢

转载自blog.csdn.net/codesWay/article/details/79987784