作者:zccst
- var scope = "global scope";
- function checkscope(){
- var scope = "local scope";
- function f(){
- return scope;
- }
- return f;
- };
- var r = checkscope()();
- console.log(r);
-
-
- var uniqueInteger = (function(){
- var counter = 0;
- return function(){
- return counter++;
- }
- }());
-
- function counter(){
- var n = 0;
- return {
- count:function(){return n++;},
- reset:function(){n=0;}
- };
- }
- var c = counter(), d = counter();
- console.log(c.count());
- console.log(d.count());
- console.log(c.reset());
- console.log(c.count());
- console.log(d.count());
-
-
- function counter(n){
- return {
- get count(){return n++;},
- set count(m){
- if(m >= n) n=m;
- else throw Error("只能越来越大,不能越来越小");
- }
- };
- }
- var c = counter(1000);
- console.log(c.count);
- console.log(c.count);
- c.count = 2000;
- console.log(c.count);
- c.count = 2000;
-
-
- function constfuncs(){
- var funcs = [];
- for(var i= 0; i<10; i++){
- funcs[i] = function(){
- return i;
- };
- }
- return funcs;
- }
- var funcs = constfuncs();
- console.log(funcs[5]());
-
- var self = this;
js-闭包(权威指南版)
原文:http://www.cnblogs.com/shsgl/p/4289934.html