javascript高级程序员必须要懂的-----面向对象的思想

JS(面向对象--模拟类、对象)

<!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>博客日志</title>

</head>

<body>
	<script>
		/*模拟一个类*/
		function Person(){
			var name="lisi";
			
		}
		//往该类中添加公共的方法
		Person.prototype.getName=function(){
			alert("method run....");
		}
		
		//创建对象
		var p=new Person();
		p.getName();
	</script>
</body>
</html>
JS(面向对象--模拟完整的类)

<!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>博客日志</title>

</head>

<body>
	<script>
		function Person(){
			//私有的静态属性
			var sex="男";
			//私有的静态方法
			function checked(sex){
				if(sex=="男"){
					alert("ok...");
				}
				else{
					alert("no no ");
				}
			}
			
			//私有属性
			this.name="lisi";
			
			//私有方法
			this.checkSex(sex){
				checked(sex);//调用私有的静态方法
			}
		}
		
		//公有静态方法
		Person.show=function(){
			alert("show run...");
		}
		
		//公有方法
		Person.prototype.getSex=function(){
			return this.sex;
		}
		
		
		
		//*************************************
		//公有静态方法调用
		Person.show();
		
		//公有方法调用
		var p=new Person();
		
		p.getSex();
		
		
		//私有方法调用
		p.checkSex("女");
		
		
	</script>
</body>
</html>
JS(面向对象--模拟封装、继承)

<!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>博客日志</title>

</head>

<body>
	<script>
		function Person(){
			this.name="lisi";
			
		}
		Person.prototype.setName=function(name){
			this.name=name;
		}
		
		Person.prototype.getName=function(){
			return this.name;
		}
		
		
		//模拟继承
		function Student(){
		
		}
		
		Student.prototype=new Person();
		
		var stu=new Student();
		
		stu.setName("wangwu");
		
		alert(stu.getName());
		
	</script>
</body>
</html>
JS(面向对象--重载、重写)

<!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>博客日志</title>

</head>

<body>
	<script>
		function Person(){
				//私有属性
				this.name = "刘德华";
			}
			function Person(name){
				this.name = name;
			}
			//共有方法
			Person.prototype.getName = function(){
				return this.name;
			}
			Person.prototype.setName = function(name){
				this.name = name;
			}
			//------------------------------------------
			//创建Student类
			function Student(){
				
			}
			
			
			//---------------------------------------------
			//让Student类继承Person类
			var p = new Person();
			Student.prototype = p;
			
			//-------------------------------
			//重写getName()、setName()方法
			Student.prototype.getName = function(){
				return "person";
			}
			Student.prototype.setName = function(name){
				alert("haha");
			}
			//-----------------------------
			var stu = new Student();
			
			stu.setName("章子怡");
			//alert (stu.getName() );

	</script>
</body>
</html>
JS(特有语句)

<!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>博客日志</title>

</head>

<body>
	<script>
		var d=new Date();
		with(d){
			alert(getFullYear()+"年"+(getMonth()+1)+"月"+getDate()+"日");
		}
		
		
		
		function Person(){
			this.name="lisi";
			this.age=20;
			
		}
		
		var p=new Person();
		
		for(x in p){
			alert(p[x]);
		}
		
	</script>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值