Javascript第五天

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Javascript第五天相关的知识,希望对你有一定的参考价值。

一、变量的作用域

<script type="text/javascript">

                 //在函数外用var声明的变量是全局变量

                 //定义在函数当中,但是没用var声明的是全局变量

                 //定义在函数当中,用var声明的是局部变量

                 function squra(num) {

                      var total; //局部变量

                      total=num*num;

                      return total;

                 }

                 var total=50; //全局变量

                 console.log(total);

                 var number=squra(20);

                 console.log(total);

 

                 var global="global";//全局变量

                 function test () {

                      var local="local";//局部变量

                      global2="global2";//全局变量

                 }

                

                 //全局变量(少用)

                 var i=1;

                 //...

                 var i=2;

                

                 function fun () {

                      console.log(i); //输出undefined

                      var i=3; //局部变量

                      console.log(i);  //输出 3

                 }

                 fun();  

                 console.log(i); //输出 2

           </script>

二、内部函数

<script type="text/javascript">

//eval 接受一个字符串类型参数,将这个字符串作为代码在上下文环境中执行,并返回执行的结果

                

                 var i=1;

                 eval("i=i+1");

                 eval("console.log(i);");

                

                 //parseInt转换成整数和parseFloat转换成浮点数

                

                 var i=parseInt(22.5);

                 var k=parseInt("1234blue");

                 console.log(i);

                 console.log(k);

                

                 var i=parseFloat("22.5float");

                 console.log(i);

                

                 //escape和unescape进行URL编码和解码

                 var url="http://www.baidu.com/s?name=小米";

                 url=escape(url);

                 console.log(url);

                 console.log(unescape(url));

                

           </script>

三、对象

1.日期对象:

<script type="text/javascript">

                      var myDate=new Date(),

                            time=myDate.getFullYear(),

                            time2=myDate.getMonth()+1,

                            time3=myDate.getDate(),

                            time4=myDate.getHours(),

                            time5=myDate.getMinutes(),

                            time6=myDate.getSeconds(),

                            time7=myDate.getDay();

                            weekly=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];

                            document.write(time+"年"+time2+"月"+time3+"日"+"&nbsp"+time4+":"+time5+":"+time6+"&nbsp"+weekly[time7]);

                 </script>

2.math对象:

<script type="text/javascript">

          

           //随机数

           //0-1  原始

                 var m=Math.random();

                 document.write(m+"<br />");

           //0-5

                 var m=parseInt(Math.random()*5);

                 document.write(m+"<br />");

           //50-100

                 var m=parseInt(50+Math.random()*50);

                 document.write(m+"<br />");

           //34-58

                 var m=parseInt(34+Math.random()*24);

                 document.write(m+"<br />");

     

                 /*var k=Math.random();

                 if(k==1){

                      alert("马尔代夫7人7天游");

                 }

                 else{

                      alert("谢谢参与");

                 }*/

                 var k=parseInt(Math.random()*10);

                 switch (k){

                      case 1:

                            alert("马尔代夫7人7天游");

                            break;

                      default:

                            alert("谢谢参与");

                            break;

                 }

                                 

                 //绝对值

                 var abs=Math.abs(-20);

                 document.write(abs+"<br />");

                

                 //四舍五入

                 var a=Math.round(4.4),b=Math.round(5.6);

                 document.write(a+"&nbsp;"+b+"<br />");

                

                 //ceil()   方法返回大于等于x的最小整数。(取大整)

                 var c=Math.ceil(1.4);

                 document.write(c+"<br />");

                

                 //floor() 方法返回小于等于x的最大整数。(取小整)

                 var d=Math.floor(1.6);

                 document.write(d+"<br />");

                

                 //返回x的y次幂

                 var e=Math.pow(4,3);

                 document.write(e+"<br />");

           </script>

3.Array 数组对象(未全):

<script type="text/javascript">

                 var weekly=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];

                 for (i=0;i<weekly.length;i++) {

                      document.write(weekly[i]+"<br />")

                 }

                

                

                 var arr=["标题一","标题二","标题三","标题四"];

                

                 arr.push("标题五","标题六");

                

                 arr.unshift("标题七","标题八");

           </script>

 

<body>

           <ul>

                 <script type="text/javascript">

                      for(j=0;j<arr.length;j++){

                            document.write("<li>"+arr[j]+"</li>")

                      }

                 </script>

           </ul>

      </body>

 

以上是关于Javascript第五天的主要内容,如果未能解决你的问题,请参考以下文章

前端基本功—javascript 第五天

JavaScript第五天之数组

JavaWeb的学习--第五天 javascript03

第五天:内置对象(7.Javascript内置对象)

二十一天——打卡第十五天(JavaScript实现登录弹出框)

__x__(79)1016第十五天__ JavaScript 对象的继承