<%@ 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>
时间: 2024-10-27 03:02:10