• 两个单体内置对象_Global和Math


    内置对象:不依赖于宿主对象,在程序执行之前就已经存在。例如Object,Array和String。

    今天主要学习另外两个单体内置对象,Global和Math。

    (一)Global 对象

    URI编码方法:

    encodeURI()方法可以对URI进行编码,主要用于整个URI。不会对本身属于URI的特殊字符进行编码,例如冒号,正斜杠,问号和井号等。

    encodeURIComponent()方法也是对URI进行编码,主要用于一段URI。会对任何非标准字符进行编码。

    例如:var uri="http://www.wrax.com/illegal value.html#start";

    alert(encodeURI(uri)); //"http://www.wrax.com//illegal%20value.html#start"       (这里的空格变成了%20)

    alert(encodeURIComponent(uri));//"http%3A%2F%2Fwww.wrax.com%2Fillegal%20value.html%23start"      (这里使用了对应编码替换所有非字母数字的字符)

    decodeURI()方法可以对encodeURI()替换的字符进行解码。

    decodeURIComponent()方法能够解码encodeURIComponent()方法编码的所有字符。

    例如:var uri="http%3A%2F%2Fwww.wrax.com%2Fillegal%20value.html%23start"

    alert(decodeURI(uri)); //"http%3A%2F%2Fwww.wrax.com%2Fillegal value.html%23start"      (这里只有%20替换回空格)

    alert(decodeURIComponent(uri));//"http://www.wrax.com//illegal value.html#start"   (所有的特殊字符的编码都替换回原来的字符)

    eval()方法值接收一个参数,可以把一个字符串当作JS表达式去执行它。

    例如:

    var num="1+1";

    var num1=eval("1+1");

    alert(num);// 1+1

    alert(num1);//2

    (二)Math对象

    min()和max()方法用于确定一组数值中最小值和最大值,可以接收多个数值参数。

    var min=Math.min(1,6,10,22,55);

    alert(min);//1

    var max=Math.max(1,6,10,22,55);

    alert(max);//55

    如果要找到数组的最大值或最小值,可以使用apply()方法:

    var num=[1,2,3,4,5];

    var max=Math.max.apply(Math,num);

    alert(max);//5

    舍入方法:

    Math.ceil()方法:执行向上舍入最接近的整数。

    Math.floor()方法:执行向下舍入最接近的整数。

    Math.round()方法:执行标准的四舍五入。

    random()方法返回大于等于0小于1的一个随机数。可以利用random()方法从某个整数范围内选择一个值  

    值=Math.random()*可能值的总数+第一个可能值

    function selectFrom(lowerValue,upperValue){

                var choice=upperValue-lowerValue+1;

                 return Math.floor(Math.random()*choice+lowerValue);

    }

    var num=selectFrom(1,10);

    alert(num); //1到10之间的随机数

  • 相关阅读:
    python 多线程测试
    python 多线程join()
    python 多线程 t.setDaemon(True):
    线程锁
    python 多线程
    模板渲染和参数传递.
    求两个数组的交集
    java数组并集/交集/差集(补集)
    java各种集合的线程安全
    页面跳转和重定向
  • 原文地址:https://www.cnblogs.com/guoxihong/p/5374212.html
Copyright © 2020-2023  润新知