javascript的apply和call,執行環境,記憶體回收,閉包

來源:互聯網
上載者:User

標籤: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,執行環境,記憶體回收,閉包

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.