標籤: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,lisivar 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>
本文出自 “matengbing” 部落格,請務必保留此出處http://matengbing.blog.51cto.com/11395502/1879398
javascript原型,組合,動態原型,穩妥建構函式式