js closure3

var closure = (function(){
    var arr = [1, 3];
    return {
        getA: (function() {
            return arr;
        }()),

        printArr: function() {
            console.log(arr);
        }
    };
}());


var firstGetArr = closure.getA;
closure.printArr(); // [1, 3]

firstGetArr[1] = 2;
closure.printArr(); // [1, 2]

var secondGetArr = closure.getA;
closure.printArr(); // [1, 2]

 

posted @ 2017-06-09 01:14  ax=null  阅读(95)  评论(0编辑  收藏  举报