JavaScript 中自定义对象

http://www.ccvita.com/94.html

http://www.iteye.com/topic/155109

 

给出了详细的说明。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>javaScaipt</title>
<script language="javascript" type="text/javascript">
var sa ={//类的定义方式1
	 userName:'userName',
	 userPass:'userPass',
	 showInfo:function(){
	 	alert("this.userName " + this.userName);
	 }
}
function funInit(){
	alert("sa.userPass " + sa.userPass);
	sa.showInfo();
	
	
	function func(){}//类的定义方式2
	func.prototype.age = 20;
	func.prototype.show = new show();//定义实例方法方式2
	var func = new func();
	alert("func.age " + func.age + "\n func.show.userName " + func.show.userName);
	
	var showObj = new show();
	//delete showObj.showInfo;删除对象的方法
	showObj.showInfo()
}
function show(){//类的定义方式3
	this.userName ='sa';
	this.userPass ='123456';
	function showNow(){//私有方法 private
		alert("userName : "+ this.userName + "\t userPass : " + this.userPass);
	}
	this.showInfo = function(){//定义实例方法方式1
		alert("show.showInfo.userName " + this.userName);
	}
}

function funOO(){
		this.funName ="123456";
		this.fun = function(){alert("funOO.fun");}
}
function funRun(){
	/*
	alert(oo.userId);  //JSON
	*/
	/*
	funOO.prototype.funName="56789";
	funOO.prototype.value="big";
	alert(funOO.prototype.funName+"\n"+new funOO().funName);//prototype 的使用
	*/
	/*
	var object = new funOO();
	object.name ="funOO";//object 是funOO 的对象
	funOO.prototype.value="big";
	alert("object.name:"+object.name+" \nfunName:"+ object.funName +"  \nprototype.value:"+ object.value);
	*/
	/*
	var object = new funOO();
	object.funName="abcd";
	var userObj = new Function();// Function 创建新的函数对象
	userObj.prototype = object;//这里prototype 把对象复制,相当于继承
	var user = new userObj();
	alert(user.funName);
	*/
	/*
	funOO.prototype.hello = new Function("{alert('hello')\;}");//Function 创建方法
	var func = new funOO();
	func.fun();
	func.hello();
	*/
}
</script>
</head>

<body onload="funInit();">
</body>
</html>

你可能感兴趣的:(JavaScript,json,XHTML,prototype,OO)