1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
<%@ 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>
//工厂模型
function
CreatePerson(id,name,age){
var
obj={};
obj.id=id;
obj.name=name;
obj.age=age;
obj.sayName=
function
(){
return
this
.name;
}
return
obj;
}
var
p1=CreatePerson(1,
'zhangsan'
,23);
//alert(p1.id);
//alert(p1.sayName());
//第二种方式:构造函数式,函数的第一个字母大写(类的模板)
function
Person(id,name,age){
this
.id=id;
this
.name=name;
this
.age=age;
this
.sayName=
function
(){
return
this
.name;
}
}
//构造一个对象,new关键字,传递参数,执行模板代码,返回对象
var
p2=
new
Person(1,
'王五'
,45);
var
p3=
new
Person(2,
'赵六'
,23);
//alert(p2.sayName());
//alert(p2===p3); //类的概念,根据模板创建出不同的对象
alert(p2.constructor==Person);
//true
alert(p3.constructor==Person);
//true
alert(p2
instanceof
Person);
//true
alert(p2
instanceof
Object);
//true
//创建对象的方式
//1.当做构造函数去使用
//var p2=new Person(1,'王五',45);
//2.普通方法的函数调用
Person(2,
'zhangsan'
,34);
//在全局环境定义属性并复制,直接定义window对象上
//在一个对象的作用域中调用
var
o=
new
Object();
Person.call(o,2,
'zhangsan'
,34);
//把Person方法绑定到o对象上
alert(o.name);
</script>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
|
本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1879238