JS练习

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>





你可能感兴趣的:(JS练习)