JS 연습

1788 단어
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>

좋은 웹페이지 즐겨찾기