1.什么是閉包
大多數(shù)書本中對閉包的定義是:“閉包是指有權訪問另一個函數(shù)作用域中的變量的函數(shù)。”。這個概念過于抽象了,對初學者而言沒啥幫助。好在《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