通过一个例子引入必闭包概念
var user={
name:'王花花';
age:20;
sex:'女'
}
user.age=21;
这样我们很轻易的改变了user的age,这种方式在大型程序中比较危险
function user(name){
var age,sex;
return{ getName:function(){return name;},setName:function(newName){name=newName}, getAge:function(){return age;},setAge:function(newAge){age =newAge; } }
}
var whh = user('王花花')
var name = whh.getName()
whh.setSex('女');
whh.setAge('22');
var sex = whh.getSex()
var age = whh.getAge()
console.log(name,sex,age)