js closure2

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:11  ax=null  阅读(107)  评论(0编辑  收藏  举报