闭包:外层函数调用时,内层函数引用着外层函数的作用域,导致外层函数作用域无法释放,形成闭包。
function parent(){
var total = 1000;
return function(money){
total -= money;
console.log("花了${money},剩余${total}");
}
}
//保护了变量不被篡改,当变量total在全局时,容易被篡改
var pay=parent();//可写可不写
pay(100);
pay(100);