Last active
December 25, 2022 21:56
-
-
Save sarink/7394867 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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