標籤:javascript的apply和call 記憶體回收 執行環境 閉包
<%@ 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>
執行環境
<%@ 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>
記憶體回收
<%@ 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>
閉包
<%@ 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” 部落格,請務必保留此出處http://matengbing.blog.51cto.com/11395502/1879217
javascript的apply和call,執行環境,記憶體回收,閉包