While I have a suspicion that there isn’t a way to make a completely untouchable JSON object in a web application, I was wondering if any of you had ways to maintain a global JSON object that could not be altered using developer tools’ consoles. I know that calling this a “private” variable isn’t the most accurate description but the restrictions that a private variable in OO languages are basically what I would like to have applied to my JSON object.
I have an application that I am developing that would benefit from keeping a savvy user from interacting with the object that I’m using for storing data in the Javascript file.
Any suggestions on how to approach this would be appreciated.
Actually you can have “private” variables in javascript and getter method to acces them:
you can do:
In this example the variable _myVariable is not accessible in the browser and not modificable by the user in any way because it’s local scope is inside the function. Your function returns an object that can access that variable because, by returning an object, you create a closure.
In this case getData is a property of the returned object and can access _myVariable because it’s local scope is that of function privateData.
I reccomend the books:
for some advanced javascript tecniques