Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Symbol only changes the property key it does not add any privacy.
The first example is equivalent to this:
// here the key is type 'string'p['s_firstname']='Nana';
The 2nd example is equivalent to this:
// here the key is type 'Symbol'p[s_firstname]='Nana';
Using the symbol will still allow you to set the firstname.
lets_firstname=Symbol();classPerson{constructor(firstName){this[s_firstname]=firstName;}log(){console.log('I am',this[s_firstname]);}}letp=newPerson("Oliver")p.log();// I am Oliverp[s_firstname]="Nana";p.log()// I am Nana
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
I am concerned about the classical way of preventing outside modification of class properties. I have been finding means to do that until I tried using symbols. In my approach, it works and that is why I wanted to share with others but I did not know that they modified using another approach as you have stated. I like how you have outlined the process. But can you help in the classical way of hiding details in JavaScript?
Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
I am not a fan of classes in JavaScript. As a matter of fact, I never create code with classes. I have found that functions will serve the same purpose. I prefer using the revealing module pattern over JavaScript classes.
Symbol
only changes the property key it does not add any privacy.The first example is equivalent to this:
The 2nd example is equivalent to this:
Using the symbol will still allow you to set the firstname.
Well noted.
You can also enumerate property-symbols with this command:
So if you do not know the symbol, you can still set the property using code like this:
Alternatively, you can also achieve privacy by switching from using a Class to using a closure.
I am concerned about the classical way of preventing outside modification of class properties. I have been finding means to do that until I tried using symbols. In my approach, it works and that is why I wanted to share with others but I did not know that they modified using another approach as you have stated. I like how you have outlined the process. But can you help in the classical way of hiding details in JavaScript?
I am not a fan of classes in JavaScript. As a matter of fact, I never create code with classes. I have found that functions will serve the same purpose. I prefer using the revealing module pattern over JavaScript classes.
Here's an example:
I added a
setName
function to demonstrate how to set the private vars_firstname
.That's cool
Okay. Thanks for the feedback