js 闭包

时间:2021-05-24 10:11:16   收藏:0   阅读:0

 

通过闭包封装私有变量

function a() {
    //函数外访问不到
    var s = 3;
   //使外部能够获得s变量的值 
    this.f = function () {
        return s
    }
    //改变s的值
    this.add=function(){
        s++
    }
}

var t = new a()
t.add()
console.log(t.f())

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!