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

javascript 学习笔记——对象、命名空间
记录以下网址的javascript core crash章节http://courses.coreservlets.com/Course-Materials/ajax-basics.html



<!-- LCTestJS_Object.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: #DDD;
}

h5{
	background-color: #FFC;
	color: #000;
	font-size: 18px;
}
</style>
<script type="text/javascript">
function printSpace(n){//输出n个空格
	if(n==null)
	document.write();
	else for(var i=0;i<n;i++)document.write('&nbsp;');
}
function print(str,description){//新起一段输出str,之后空6格,再输出description
	document.write("<p>"+str);
	printSpace(6);
	if(description!=null)
		document.write("//"+description);
}
</script>
</head>
<body>
	
	<h2>测试javascript的对象 version: 2012_01_11</h2>
	
	<h3>构造函数</h3>
	<h4>构造函数内成员变量必须用this指定;构造函数外可以追加成员变量(或函数)</h4>
	<h5>
		function Circle(r){
		<br>	this.radius=r;//构造函数内成员变量必须用this指定;
		<br>	//成员函数,每个实例一个,耗内存啊(不推荐)
		<br>	this.getAreaVer1=function(){//this都是必须写的!!!
		<br>		return Math.PI*this.radius*this.radius;
		<br>	}
		<br>	//类变量,不是全局的?
		<br>	Circle.prototype.radiusClass=5;
		<br>	//类函数,注意前面是类名(Circle)而非this!!!实例共享一个
		<br>	Circle.prototype.getArea=function(){//this都是必须写的!!!
		<br>		return Math.PI*this.radius*this.radius;
		<br>	}			
		<br>}
		<br>	var c=new Circle(1);		var c2=new Circle(2); c2.radiusClass=3;
	</h5>
	<script type="text/javascript">
		function Circle(r){
			this.radius=r;//构造函数内成员变量必须用this指定;
			//成员函数,每个实例一个,耗内存啊(不推荐)
			this.getAreaVer1=function(){//this都是必须写的!!!
				return Math.PI*this.radius*this.radius;
			}
			//类变量,不是全局的?
			Circle.prototype.radiusClass=5;
			//类函数,注意前面是类名(Circle)而非this!!!实例共享一个
			Circle.prototype.getArea=function(){//this都是必须写的!!!
				return Math.PI*this.radius*this.radius;
			}			
		}

		var c=new Circle(1);		
		c.radiusClass=3;var c2=new Circle(2);
		document.write("<p>c.radiusClass="+c.radiusClass);
		document.write("<p>c2.radiusClass="+c2.radiusClass+"\t//没变啊!");
		
		document.write("<p>c.getArea()=\t"+c.getArea());
		document.write("<p>c2.getArea()=\t"+c2.getArea());
		
	</script>
	
	<!--        ----------------------         -->
	<h3>静态方法、命名空间(用类的函数模拟)</h3>
	<h4></h4>
	<h5>
		var obj1={p:1};    var obj2={p:2};
		<br>function minus(x,y){return (this.p+x-y);}
	</h5>
	<script type="text/javascript">
		var Util={};//这样就可以当做命名空间了!!
		Util.minus=function(x,y){return x-y;}
		Util.add=function add(x,y){return x+y};//两个处add
		print("(typeof Util.add)="+(typeof Util.add));
		print("(typeof Util.minus)="+(typeof Util.minus));
		print("(typeof add)="+(typeof add),"囧啊,还是没定义;但据说调试器可见");
		print("(typeof minus)="+(typeof minus));
		print("Util.minus="+Util.minus);
		print("Util.add="+Util.add,"有函数名,写两遍add还是有用滴");
	</script>
	
	<!--        ----------------------         -->
	<h3>JSON (JavaScript Object Notation)</h3>
	<h4>可用于可变参数函数</h4>
	<h5>var someObject = { property1: value1,property2: value2,... };
	<p>function test(x){
	<br>		if(x.property1!=null)...
	<br>	}
	</h5>
	<script type="text/javascript">
		
		document.write("<p>minus.call(obj1,9,4)=" + minus.call(obj1,9,4));
	</script>
	
</body>
</html>