function Liu(name,wife){
var preWife = "xiaozhang";
this.name = name;
this.wife = wife;
this.divorce = function(){
this.wife = preWife
}
this.changePreWife = function(target){
preWife = target
}
this.sayPreWife = function(){
console.log(preWife)
}
}
var liu = new Liu('liu','dai')
console.log(liu.preWife)//undefined
console.log(liu.sayPreWife())//xiaozhang
// prewWife并不是自己身上的变量,所以第一行打印出来是undefiend,但是由于函数内部形成了闭包,函数A内部的函数BCD共用了函数A的AO,所以能操作操作preWife,因此preWife变成了liu的私有化变量
闭包作用:私有化变量
最新推荐文章于 2021-06-02 16:27:28 发布