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
|
<%@ 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
sum(a,b){
return
a+b;
}
function
call1(a,b){
return
sum.call(
this
,a,b);
}
function
apply1(a,b){
return
sum.apply(
this
,[a,b]);
//后面是一个数组
}
//alert(call1(10,20));
//alert(apply1(1,2));
//扩充函数的作用域
window.color=
'red'
;
var
obj={color:
'blue'
};
function
showColor(){
alert(
this
.color);
}
//showColor(this.color); //输出red,因为当前对象是window
//showColor(obj); //输出red 因为调用者是window对象,showColor中的this指向调用者
//showColor.call(obj); //blue call函数扩展了作用域,this指向传入的对象obj
function
test(a,b){
return
a+b;
}
//自定义对象
function
Obje(a,b){
this
.a=a;
this
.b=b;
return
a*b;
}
var
o=
new
Obje(10,20);
o.method=test;
alert(o.method(o.a,o.b));
//相当于alert(test.call(o,o.a,o.b));
delete
o.method;
//alert(test.call(o,o.a,o.b)); //输出30 即a+b
</script>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
|
执行环境
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
|
<%@ 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>
//1.执行环境window对象(最上层的执行环境)
var
color1=
'red'
;
function
changeColor(){
//每一个函数都有一个执行环境(variable obj)
var
color2=
'blue'
;
function
swap(){
//这个函数又产生了一个执行环境(variable obj)
var
color3=color2;
color2=color1;
color1=color3;
//这里可以访问color1,2,3,
//color1:一级作用域,color2:二级作用域,color2:三级作用域
}
//这里可以访问color1,2,不能访问color3
swap();
}
//环境变量,可以一层一层的向上追溯,访问它的上级环境(变量和函数)
//注意逐层向上
changeColor();
//作用域window,第一个作用环境
//这里只能访问color1
</script>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
|
垃圾回收
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
|
<%@ 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>
//javascript具有自动垃圾回收机制,离开作用域的变量标记为可以回收,在垃圾回收期间被回收
//垃圾收集方法1.标记,2.引用计数法
function
test(){
var
a=10;
//标记使用 count=1;
var
b=20;
//标记使用 count=1;
var
c=a;
//count=2;
a=20;
//count=1;
}
//test(); //执行完a,b再次标记为没有被使用
//块级作用域
// function test2(){
// for(var i=0;i<3;i++){
// alert(i);
// }
// alert(i); //javascript没有块级作用域的概念,会打印出3
// }
// test2();
//js:()表示执行
function
test2(){
(
function
(){
for
(
var
i=0;i<3;i++){
alert(i);
}
})();
//定义了一个内名函数,单独做一个作用域,并立即执行,执行完作用域被回收
//alert(i); //undefined
}
test2();
(
function
(){alert(
"立即执行"
);})();
//单独做一个作用域,立即执行
</script>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
|
闭包
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
|
<%@ 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>
var
name=
"张三"
;
var
obj={
name:
"王五"
,
getName:
function
(){
return
function
(){
return
this
.name;
}
}
};
// alert(obj.getName()); //obj.getName()返回的是一个函数 即 function(){return this.name;}
// //alert(obj.getName()());
// var k=obj.getName(); //全局作用域
// alert(k()); //等同于alert(window.k()); 张三
var
name=
"张三"
;
var
obj={
name:
"王五"
,
getName:
function
(){
//this总是指向调用者
var
o=
this
;
//o保存了this
return
function
(){
return
o.name;
}
}
};
//alert(obj.getName()); //obj.getName()返回的是一个函数 即 function(){return this.name;}
//alert(obj.getName()());
//var k=obj.getName(); //全局作用域
//alert(k()); //王五
//闭包:一个函数可以访问另一个函数作用域中的变量
//封闭性:相当于java中的private保护变量
//1
function
f(x){
//2
var
temp=x;
//被标记为未被使用
return
function
(x){
//3 function有了一个执行域
temp+=x;
//在此处,第三级引用了第二级的变量,又被标记为被使用,不会被回收
alert(temp);
}
}
var
a=f(50);
alert(a);
a(5);
a(10);
</script>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
|
本文转自 matengbing 51CTO博客,原文链接:http://blog.51cto.com/matengbing/1879217