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
77
78
79
80
81
|
<%@ 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>
//obj.getPrototypeOf根据实例对象获得原型对象
function
Person(){
}
Person.prototype.name=
'张三'
;
Person.prototype.age=
'21'
;
Person.prototype.sayName=
function
(){alert(thsi.name);}
var
p1=
new
Person();
//alert(p1.name);
//var prototypeObj=Object.getPrototypeOf(p1);
//alert(prototypeObj===Person.prototype);
p1.name=
'王五'
;
//每次代码读取对象属性时,首先会进行一次搜索,搜索实例对象里name的属性,看看有没有,如果没有,
//然后再去p1的实例所对应的原型搜索,有就返回,没有返回undefined
//alert(p1.name);
//delete p1.name;
//alert(p1.name); //清除添加的name //获得原型对象的属性
//判断一个对象属性属于原型属性还是实例属性
var
p3=
new
Person();
var
s=p3.hasOwnProperty(
'name'
);
//alert(s); //false
p3.name=
'赵六'
;
//alert(p3.hasOwnProperty('name')); //true
//in操作符 判断属性是否存在于实例对象和原型对象(无法区分在实例还是原型)
var
p4=
new
Person();
var
p5=
new
Person();
//alert('name' in p4); //true
p5.name=
'旺旺'
;
//alert('name' in p5);
//在原型对象中是否存在属性
function
hasOwnPrototypeBySelf(object,name){
return
!object.hasOwnProperty(name) && name
in
object;
}
//alert(hasOwnPrototypeBySelf(p4,'name')); //true
//alert(hasOwnPrototypeBySelf(p5,'name')); //false
//Object.keys()拿到当前对象中所有的属性
var
p6=
new
Person();
p6.sex=
'男'
;
//alert(Object.keys(p6));
//alert(Object.keys(Person.prototype));
//constructor属性,该属性不能被枚举出来
//Object.getOwnPropertyNames()获取所有属性 不关该属性是否可以被枚举
alert(Object.getOwnPropertyNames(Person.prototype));
</script>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
|
本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1879309