0

Is it possible to create "private static" variables in JavaScript similar to Java?

My scenario is that I need to retrieve a data point from server and save in the static variable, so that my function which uses this need not make server call every time its called.

I also need the data once fetched and copied to variable not be read outside that class as I need it to be secure, want to have it private. I have read that to achieve private variable we use scope functionality in JavaScript but I also need it to be static.

Can this be achieved?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
user1737619
  • 247
  • 1
  • 2
  • 15
  • 5
    You're not describing a static variable, you're just describing a cache. And yes it's achievable. As for making variables private it's not inherently possible but you can use closure scopes to make something that works the same way and achieves the same result. However, we will need examples of your code to answer either of those questions. – casraf Sep 22 '16 at 13:09
  • I'm not describing a cache because a cached variable can be edited if someone knows the key. I dont want it to be modified from outside – user1737619 Sep 22 '16 at 13:21
  • @user1737619: The linked dupe targets explain how the "private" and "static" concepts can be implemented in JavaScript. Read those, and roll out your own solution. Besides, the two concepts are unrelated, so including both in 1 question makes the question "Too broad". – Cerbrus Sep 22 '16 at 13:49

0 Answers0