现在的位置: 首页 > 综合 > 正文

JS练习

2018年06月07日 ⁄ 综合 ⁄ 共 1095字 ⁄ 字号 评论关闭

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>

【上篇】
【下篇】

抱歉!评论已关闭.