Reputation: 1296
While doing javascript applications , I use the below convention to write my code.
I use a single javascript object to hold all my functions and runtime information.
This is a simple use case scenario for an example, Do not consider it as functional code.
var main = {
functOne: function(val){
main.functTwo(parseInt(val));
},
functTwo: function(data){
main.runtimeData = data;
}
}
Usage : <button onclick="main.functOne('123')">Click Me!</button>
This otherwise works ok but it is possible to view/change the properties and runtime information from the browser's console by accessing the object main.runtimeData .
So how can I restrict that kind of access without hindering the functionality?
Please add examples of some similar code if you can.
Upvotes: 1
Views: 70
Reputation: 23502
On a modern browser you could use freeze
to give some hindrance.
The Object.freeze() method freezes an object: that is, prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed. In essence the object is made effectively immutable. The method returns the object being frozen.
Upvotes: 2
Reputation: 56
Javascript is always "in the hands of the enemy". Having said that, a module pattern might help you hide away some functions. Edit: to reiterate, this is not secure or private, it's all in your user's hands.
var main = (function() {
var x = {};
// Private
var private_var = "foo";
// Public
x.get_var = function() {
return private_var;
}
return x;
}());
Very crude example, but it might help you. More info at adequatelygood.com
Upvotes: 1