JavaScript Security

by Daniel Lamb

JavaScript

var table = (function () {
    var array = [];
    return {
        get: function (i) {
            return array[i];
        },
        store: function (i, v) {
            array[i] = v;
        },
        append: function (v) {
            array.push(v);
        }
    };
}());

//looks great and works as it should
//the private variable 'array' seems pretty secure
table.append('test');
console.log(table.get(0));

//until this happens: use 'store' to change the 'push' method
table.store('push', function() {
    alert('all your bases are belong to us');
    //Remember: "this" referrs to the execution context not the delaration context.
    console.log('I now have the private variable', this);
});
table.append();