Skip to content

Instantly share code, notes, and snippets.

@sarink sarink/private.js

Last active Jun 24, 2017
Embed
What would you like to do?
// Sample demo of how to gain access to "private" variables in JavaScript,
// even if they'd normally be inaccessible due to the closure.
var Table = function () {
var _array = ["super", "secret", "message"];
return {
get: function (i) { return _array[i]; },
store: function (i,v) { _array[i] = v; },
append: function (v) { _array.push(v); }
};
};
var t = new Table(); // how would we ever gain access to the secret private internal `_array_`??
var stolenArray;
t.store("push", function () { stolenArray = this; });
t.append("whocares");
console.log(stolenArray); // ["super", "secret", "message"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.