Make Object Properties Private

Make Object Properties Private
0.0 0

#1

Help please!my code is crashed


#2

we’d love to help, but we need a bit more information. Preferably including a link to your codepen or a copy/paste of your code.


#3

What object? What is the language you are using? Java: add “private” in front of declaration of the field you want to make private. Example : private String privateString = “this is private”; . For javascript it will be a bit tricky. You might have to wrap it in something like a function. function stuffInsideIsPrivate(){var thisIsPrivate = “private”;};.


#4

You can do like this to make it private;

let obj = function() {

let p = ‘I am private variable’;
return {
getPrivate: () => {
console.log§;
}
};
}();

obj.getPrivate();