javascript函数是将要执行的代码以及执行这些代码的作用域和作用域的arguments一起构成的一个综合体,即使函数包含相同的javascript代码,并且每段代码都是从相同的作用域调用的,还是可以返回不同的结果的。
因为javascript中的函数是在当时定义它们的作用域里运行的,而不是在执行它们的作用域里运行的。
这种代码和作用域的综合体叫闭包。所有的javascript函数都是闭包。
uniqueID = (function() { // The call object of this function holds our value var id = 0; // This is the private persistent value // The outer function returns a nested function that has access // to the persistent value. It is this nested function we're storing // in the variable uniqueID above. return function() { return id++; }; // Return and increment})();// Invoke the outer function after defining it, and return a function: function() { return id++; }console.log(uniqueID());// alert(function() { return id++; }());console.log(uniqueID());console.log(uniqueID()); |
当一个嵌套函数被导出到它所定义的作用域外时,这种闭包才有意思。当一个嵌套的函数以这种方式使用时,通常被明确的叫做一个闭包。
uniqueID的函数体为function() { return id++; },它是从一个function literal中返回得到,并包含了导出后的作用域,包含了变量名和值等,也就是从这个匿名函数是返回了一个闭包。
在uniqueID被函数运算符()调用时,已经在函数定义的作用域外,所有调用操作会影响闭包内的变量并仍会被此闭包继续保存。
Ruby和Perl中有个lambda方法也可以生成一个闭包。
更多关于javascript的闭包说明请查看此处