JS练习

来源:互联网 发布:java二维数组的遍历 编辑:程序博客网 时间:2024/05/04 18:30

1.将函数用作方法

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>html5 Web Storage的localStorage完成的网站计数器功能</title></head><body>    <script type="text/javascript">    var myData={name:"cheng",weather:"sunny",printMessages:function(){document.writeln("hello"+this.name+".");document.writeln("today is "+ this.weather+".");}};myData.printMessages();    </script>  </body></html>

2.枚举数组内容

<script type="text/javascript">//创建和填充数组var myArray=[100,200,true]myArray[1]="change";for(var i=0;i<myArray.length;i++){document.writeln(myArray[i]);}    </script>

3.异常捕获

    <script type="text/javascript">try {var myArray=[100,200,true];var myArray1=myArray;for(var i=0;i<myArray1.length;i++){document.writeln(myArray1[i]);}}catch(e){document.writeln("error"+e);}finally{document.writeln("state")}    </script>
4.检查变量或属性是否为undefined或null

    <script type="text/javascript">var mydata={name:"cheng",city:null};if(!mydata.name){document.writeln("name is null or undefined");}else{document.writeln("name is not null or undefined");}document.writeln("<br>");if(!mydata.city){document.writeln("city is null or undefined");}else{document.writeln("city is not null or undefined");}    </script>





0 0