javascript 学习笔记——载入;注释;rue、false的判断;for语句、数组的声明与使用

记录以下网址的javascript core crash章节 http://courses.coreservlets.com/Course-Materials/ajax-basics.html


1、载入
/*方法1、从外部载入;适合函数定义*/
 <script src="my-script.js" type="text/javascript"></script> 
/*方法2、直接在html中写出;直接执行!!!*/
 <script type="text/javascript">JavaScript code</script>


2、注释
同Java一样,
//行注释
/*   块注释   */


3、条件语句

true和false和Java不同
引用
以下条件被当成“false” : false  null  undefined  "" (empty string)  0  NaN false : false, null, undefined,    (empty string), 0, NaN
以下条件被当成“true”: anything else (including the string "false")


4、数组、for循环
先看结果:


<!-- LCTestForLoop.html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
h3{
	color: #FFF;
	background-color: #09F;
	font-style: normal;
	font-weight: bold;
}
h4 {
	font-weight: bolder;
	color: #d00;
	background-color: #CCC;
}
</style>

<title>javascript数组及for语句测试</title>
</head>
<body>

	<h3>遍历对象的属性,for/in语句; 注意拿出的是下标!!</h3>
	<h4>var person = {	firstName : "Brendan",lastName : "Eich"	};//声明对象</h4>
	<script type="text/javascript">
		var person = {	firstName : "Brendan",lastName : "Eich"	};
		for ( var property in person) {
			document.write("<p>" + person[property]);
		}
	</script>

	<h3>for语句;数组的声明、遍历——数组为:var arrayInt = [ 5, 4, 2,2];</h3>
	<h4>常规方法 </h4>
	<script type="text/javascript">
		var arrayInt = [ 5, 4, 2,2];//一维数组的声明用方括号 ,下标从0开始
		for(var ind=0;ind<arrayInt.length;ind++){
			document.write("\t"+arrayInt[ind]);
		}
	</script>
	
	<h4>注意:for/in语句的自变量为下标(不推荐此方法)——数组为:var arrayInt = [ 7, 4];<p> 覆盖了上面的变量;此处不声明将沿用上面的!! </h4>
	<script type="text/javascript">
		var arrayInt = [ 7, 4];//一维数组的声明用方括号 ,下标从0开始;此处的定义覆盖了上面的
		for ( var ind in arrayInt) {//不推荐
			document.write("<p>下标:"+ ind);
			document.write(";\t\t\t内容:" + arrayInt[ind]);
		}
	</script>

	<h3>动态数组</h3>
	<h4>var array = [0];array[4]=8;//动态调整数组</h4>
	<script type="text/javascript">
		var array = [0];
		array[4]=8;//动态调整数组
		document.write("数组内容:");
		for(var ind=0;ind<array.length;ind++){
			document.write("\t"+array[ind]);
		}
		document.write("<p>array[1]==null?\t"+(array[1]==null));
	</script>
</body>
</html>

猜你喜欢

转载自cherishlc.iteye.com/blog/1340871