【JavaScript高级程序设计】8、单体内置对象

1、URI编码方法

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
    
    <script type="text/javascript">
    
        var uri = "http://i.cnblogs.com/EditArticles.aspx ?opt=1";

        //这里用两个函数来对uri进行编码,如果uri是不符合浏览器的话可以借助这个函数使uri来适应浏览器
        //http://i.cnblogs.com/EditArticles.aspx%20?opt=1
        alert(encodeURI(uri));

        //http%3A%2F%2Fi.cnblogs.com%2FEditArticles.aspx%20%3Fopt%3D1
        alert(encodeURIComponent(uri));

    </script>

 </head>
 <body>
  
 </body>
</html>

结果:

 

还有对应的解除编码之后的uri函数decodeURI和decodeURIComponent

2、eval方法 

 

这个函数用来接收将要执行的JavaScript字符串

例如

eval("alert('hi')");

这个就相当于

alert("hi");

 

3、Math对象

     var max = Math.max(3, 43, 232, 44, 1231);
        alert(max);

        //产生随机数
        //公式:值 = Math.floor(Math.random() * 可能值的总数 + 第一个可能的值)
        var nums = new Array();
        var num;    //产生1到10 的随机数
        var num2 = Math.floor(Math.random() * 10 + 2);     //2到10
        for(var i = 0; i < 10; ++i)
        {
            num = Math.floor(Math.random() * 10 + 1);
            nums.push(num);
        }//for
        

        alert(num + "第二个数" + num2);
        alert(nums.join("$"));

 

结果:

 

posted @ 2016-07-07 15:09  cutter_point  阅读(137)  评论(0)    收藏  举报