When you try to stringify any object, you only get like a json the public properties but, if my object have private properties, how I can save it?
-
please share the object which u want to store in storage – brk Dec 31 '17 at 03:22
-
It's only a question, I'm not trying it yet – Daniel Dec 31 '17 at 03:24
-
Show us what you mean by private properties. – Dec 31 '17 at 03:25
-
But, if you want, for example an object person with the private attribute age and the public attribute name – Daniel Dec 31 '17 at 03:25
-
@Daniel There are no access modifiers on JSON objects, neither in JavaScript. – Adrian Dec 31 '17 at 03:31
-
// Private: var name = "Dani"; //public: this.name = "Dani" ; – Daniel Dec 31 '17 at 03:32
-
Oh jeeze. edit your question and paste code there, without the encoding. – Dec 31 '17 at 03:37
3 Answers
Here is an example with a constructor for a Person object. In this case, We override the toJSON method of this object to include the private variable "age". The toJSON method is used internally by JSON.stringify
function Person(name, age) {
var age = age // Only accessible in the scope on the constructor
this.name = name
this.toJSON = function() {
return {age: age, name: this.name}
}
}
// eg with localStorage
var john = new Person('John', 40)
localStorage.setItem('John', JSON.stringify(john))
I must add as commented previously that there is no such thing as a private property in javascript : only scoped variables and closures as getter/setter to make them accessible to the outside

- 71
- 4
-
-
I think its worth noting that once the object is pulled out of local storage, the private members are no longer private. Nothing can be done about this, as far as I know. Still, this is the best answer at this time. – Dec 31 '17 at 04:24
Currently, there are no such things as private
members (properties, methods) on Javascript objects. An ECMAScript proposal will bring private fields to a future version of Javascript. However, those private fields will be strictly accessible from within the scope of the object. It will be impossible to access them from outside the scope of the object, or even know about their presence.
If you are defining your own objects, create a toJSON()
method and return the string representation of the object however you wish.

- 9,284
- 5
- 34
- 43
All javaScript properties are public; local storage stores key:value pairs that are only retrievable by the domain that has stored them, that is if cookies are enabled! It sounds like from the question you are trying to find out a way to have persistent data storage on a "client side" scripting language, the answer is there is no guarantee of persistent data existing on a client machine without other technologies, for system security; access to a client machine's storage system is severely restricted. Local Storage is only able to store a limited amount of data that is restricted by the browser and operating system that owns it.

- 120
- 6
-
"on a "client side" scripting language" You mean the language the question is tagged with: JavaScript? – Dec 31 '17 at 03:49
-
Yes, it is a client side scripting language unless you are using node.js! I put it in quotes to emphasize the fact that it is a client side scripting language. – Matthew Lagerwey Dec 31 '17 at 04:08