日期:2014-05-16  浏览次数:20338 次

javascript 学习笔记——函数
记录以下网址的javascript core crash章节http://courses.coreservlets.com/Course-Materials/ajax-basics.html
1、

(代码统一放在后面)
2、高级


3、特殊函数



源代码1
<!-- LCTestJS_functions.html  version: 2012_01_11 -->
<!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">
<title>测试javascript的函数 version: 2012_01_11</title>
<style>
h3 {
	color: #FFF;
	background-color: #09F;
	font-style: normal;
	font-weight: bold;
}

h4 {
	font-weight: bolder;
	color: #d00;
	background-color: #CCC;
}

h5{
	background-color: #FFC;
	color: #000;
	font-size: 18px;
}
</style>
</head>
<body>
	<h2>测试javascript的函数 version: 2012_01_11</h2>

	<h3>函数赋值给变量(也可以赋值给对象的成员变量)</h3>
	<h5>
		function square(x){return x*x;}
		<br>var funcVar=square;
		<br>或者 var funcVar=function(x){return (x*x);};
	</h5>
	<script type="text/javascript">
		function square(x) {
			return x * x;
		}
		//var funcVar = square;
		var funcVar=function(x){return (x*x);};
		document.write("<p>funcVar(5)=" + funcVar(5));
	</script>
	
	<!--        ----------------------         -->
	<h3>函数数组</h3>
	<h5>
		function sqrt(x){return Math.sqrt(x);}
		<br>var funcArray=[square,sqrt];
	
	</h5>
	<script type="text/javascript">
		function sqrt(x){return Math.sqrt(x);}
		//var funcVar2 = sqrt;
		var funcArray=[square,sqrt];
		document.write("<p>funcArray[0](4)=" + funcArray[0](4));
		document.write("<br>funcArray[1](4)=" + funcArray[1](4));
	</script>
	
	<!--        ----------------------         -->
	<h3>函数作为其他函数的参数</h3>
	<h5>
		function func1SubFunc2(func1,func2,x){return func1(x)-func2(x);}
	</h5>
	<script type="text/javascript">
		function func1SubFunc2(func1,func2,x){return func1(x)-func2(x);}
		document.write("<p>func1SubFunc2(square,sqrt,4)=" + func1SubFunc2(square,sqrt,4));

	</script>
	
	<!--        ----------------------         -->
	<h3>函数作为返回值</h3>
	<h5>
		function returnFunc(x){if(x==1)return square;else return sqrt;}
	</h5>
	<script type="text/javascript">
		function returnFunc(x){if(x==1)return square;else return sqrt;}
		document.write("<p>returnFunc(1)(4)=" + returnFunc(1)(4));
		document.write("<p>returnFunc(2)(4)=" + returnFunc(2)(4));
	</script>
	
	<!--        ----------------------         -->
	<h3>匿名函数</h3>
	<h5>
		 (function(x) {return(x+7);})(10); -> ? 17
	</h5>
	<script type="text/javascript">
		document.write("<p>(function(x) {return(x+7);})(10)=" + (function(x) {return(x+7);})(10));
	</script>
	
</body>
</html>

源代码2
<!-- LCTestJS_functionsAdvanced.html  version: 2012_01_11 -->
<!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">
<title>测试javascript的函数 (高级) version: 2012_01_11</title>
<style>
h3 {
	color: #FFF;
	background-color: #09F;
	font-style: normal;
	font-weight: bold;
}

h4 {
	font