首页 > 代码库 > JS閉包研究

JS閉包研究

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <script>        //------------------------------------------------------------------        var intCount = 0;        function Add() {            return intCount++;        }        //------------------------------------------------------------------        var AddPkgErr = function () {            var intCount = 0;            AddPkgErr.prototype.Add = function () {                return intCount++;            }        }        //------------------------------------------------------------------        var AddPkgOK = function () {            //定義包的變量            AddPkgOK.prototype.intCount = 0;            //定義包的方法            AddPkgOK.prototype.Add = function () {                return intCount++;            }        }        //====================================================================        function test1()        {            WriteLog("=====================================");            WriteLog("非閉包,每次累加:" + Add());        }        function test21() {            WriteLog("=====================================");            var objAddPkg = new AddPkgErr();            WriteLog("閉包單組21,每次都是新的:" + objAddPkg.Add());        }        function test22() {            WriteLog("=====================================");            var objAddPkg = new AddPkgOK();            WriteLog("閉包單組22,會累加:" + objAddPkg.Add());        }        function test3() {            WriteLog("=====================================");            WriteLog("閉包多組,每組都是新的並且組與組之間互不影響");            var objAddPkg1 = new AddPkgErr();            WriteLog("第一組,第一次:" + objAddPkg1.Add());            WriteLog("第一組,第二次:" + objAddPkg1.Add());            WriteLog("第一組,第三次:" + objAddPkg1.Add());            WriteLog("=====================================");            var objAddPkg2 = new AddPkgErr();            WriteLog("第二組,第一次:" + objAddPkg2.Add());            WriteLog("第二組,第二次:" + objAddPkg2.Add());            WriteLog("第二組,第三次:" + objAddPkg2.Add());            WriteLog("=====================================");            WriteLog("第一組,第四次:" + objAddPkg1.Add());            WriteLog("第二組,第四次:" + objAddPkg2.Add());        }        function test4() {            WriteLog("=====================================");            WriteLog("閉包多組,每組都是新的並且組與組之間互不影響");            var objAddPkg1 = new AddPkgOK();            WriteLog("第一組,第一次:" + objAddPkg1.Add());            WriteLog("第一組,第二次:" + objAddPkg1.Add());            WriteLog("=====================================");            var objAddPkg2 = new AddPkgOK();            WriteLog("第二組,第一次:" + objAddPkg2.Add());            WriteLog("第二組,第二次:" + objAddPkg2.Add());            WriteLog("=====================================");            WriteLog("第一組,第三次:" + objAddPkg1.Add());            WriteLog("第二組,第三次:" + objAddPkg2.Add());        }        function WriteLog(sMsg) {            txtMsg.innerHTML = txtMsg.innerHTML + sMsg + "\n";        }    </script></head><body>    <input id="btnTest1" type="button" onclick="test1();" value="非閉包" />    <input id="btnTest2" type="button" onclick="test21();"  value="閉包單組" />    <input id="btnTest3" type="button" onclick="test22();" value="閉包單組" />    <input id="btnTest4" type="button" onclick="test3();" value="閉包多組多次(錯誤結果)" />    <input id="btnTest5" type="button" onclick="test4();" value="閉包多組多次(預期結果)" />    <input id="btnClear" type="button" onclick="txtMsg.innerHTML = ‘‘;" value="清空" />    <br />    <textarea id="txtMsg" rows="2" cols="20" style="width:100%; height:600px;"></textarea></body></html>

 

JS閉包研究