javascript原型,組合,動態原型,穩妥構造函數式

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'test3.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script src="js/jquery-2.1.1.min.js"></script>
	<script type="application/javascript">
	//原型的概念:原型對象裏的全部屬性和方法被全部構造函數實例化出來的對象所共享
//	function Person(){
//		
//	}
//	Person.prototype={
//		constructor:Person,
//		name:'程序員',
//		age:21,
//		friends:['wangwu','zhaoliu'],
//		say:function(){
//			alert(this.friends);
//		}
//	}
	//原型裏的屬性和方法被全部對象共享
	/*
	var p1=new Person();
	p1.friends.push('lisi');
	p1.say();                        //wangwu,zhaoliu,lisi
	var p2=new Person();
	p2.say();                        //wangwu,zhaoliu,lisi ,p1添加friends,p2也添加了friends
	*/
	
	//組合模式:通常使用原型和構造函數
	/*
	function Person1(name,age,friends,job){
		this.name=name;
		this.age=age;
		this.friends=friends;
		this.job=this.job;
		
	}
	Person1.prototype={
		constructor:Person1,
		sayName:function(){
			alert(this.name);
		}
	}
	
	
	var p3=new Person1('zhangsan',21,['wangwu','zhaoliu'],'程序員');
	var p4=new Person1('lisi',21,['wangwu','zhaoliu'],'程序員');
	p3.sayName();
	p4.sayName();
	
	*/
	
	
	
	//動態原型模式(讓你的代碼封裝到一塊兒)
	/*
	function Person2(name,age,friends,job){
		this.name=name;
		this.age=age;
		this.friends=friends;
		this.job=this.job;
		//動態原型方法
		if(typeof this.sayName !='function'){
			//
			Person2.prototype.sayName=function(){          //sayName方法只建立一次
				
				alert(this.name);
			}
		}
		
	}
	var p5=new Person2('zhangsan',21,['wabgwu','zhaoliu'],'chengxuyuan');
	p5.sayName();
	
	*/
	
	//穩妥構造函數式   durable object(穩妥對象) 很是安全的環境中
	//1.沒有公共屬性,
	//其餘對象中也不引用this對象,不能使用this關鍵字
	function Person3(name,age,job){
		var object={};
		//能夠定義私有的方法和屬性
		var name=name;
		var age=23;
		var job='程序員';
		object.sayName=function(){
			alert(name);
		}
		return object;
	}
	
	
	var p7=new Person3('zhangsan',21,'chenuxyuan');
	p7.sayName();
	
	
	
	</script> 
	
  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>
相關文章
相關標籤/搜索