<%@ 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>
執行環境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> //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>
垃圾回收css
<%@ 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>
閉包html
<%@ 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>