Skip to content

Instantly share code, notes, and snippets.

@sarink
Last active December 25, 2022 21:56
Show Gist options
  • Save sarink/7394867 to your computer and use it in GitHub Desktop.
Save sarink/7394867 to your computer and use it in GitHub Desktop.
// 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