1.什么是闭包

大多数书本中对闭包的定义是:“闭包是指有权访问另一个函数作用域中的变量的函数。”。这个概念过于抽象了,对初学者而言没啥帮助。好在《Javascript忍者秘籍》5.1中给了一个例子来进一步的解释了什么是闭包:

大学生就业培训,高中生培训,在职人员转行培训,企业团训

            var outerValue= 'ninja';            
            var later;            
            function outerFunction() {                var innerValue = "samurai";                
                function innerFunction(paramValue) {
                    assert(outerValue == "ninja", "I can see the outerValue.");
                    assert(innerValue == "samurai", "I can see the innerValue.");
                    assert(paramValue == "wakizashi", "I can see the paramValue.");
                    assert(tooLater == "ronin", "Inner can see the tooLater.&quo
        
		

网友评论