Classes are now supported in JavaScript(ES6 feature). One key important of classes is bundling methods and data together for specific objects. Henc...
For further actions, you may consider blocking this person and/or reporting abuse
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.
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
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:
Well noted.
"Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them."
You're basing your argument on the wrong premises:
Yes, encapsulation is - among other things - used to prevent direct access to a property or method, but this has nothing to do with "authorization" (another common misconception).
The whole idea here is to hide the details in a class so they cannot be changed outside class. It is obvious that using the approach I propose, they cannot be changed outside the class, however,@joelnet , outlined means that they can be accessed outside the class and then modified, which refutes the article.
I'm sorry this will seem like nitpicking, but I believe concepts are truly important: every property, unless static (which I don't believe JS has, but I might be wrong), is always being accessed "outside the class", since they are being accessed in an object, and not the class itself.
My goal is not the final result you wish (protecting the properties), but just to set OO straight.
Sammy Israwi is absolutely correct: the way to do it in OO is through what we call visibility (setting a property or method as public, protected or private), which seems to be in ES' future.
No worries. I appreciate the way, we are tackling the flaws though. That is a good way of critiquing an article.
Thanks for the feedback too.
Cool concept, I didn't know symbols had made their way into JS. Shame you have to explicitly invoke them with a constructor and there isn't some sugar like in ruby / scala
WRT to data hiding encapsulation, not sure if you've seen the draft spec for private properties / methods. The syntax is ugly, but nice to finally get it in.
github.com/hemanth/es-next#orthogo...
Here is my two cents when it comes to private members.
But then again, this is just a personal preference.
Fantastic! I didn't know about Symbols before.
But why not use Private Fields? (which has been merged into the Class Fields proposal).
It's not yet implemented, but it is on Stage 3 so hopefully, it will be coming soon!
Most often _ is prefixed before the properties to signify private properties but they can still be modified outside the class.
Thanks for the feedback and I will look into Private Fields
That's because prefixing something with an underscore is a convention and not a language feature. In order to truly protect object properties you need visibility, as pointed by Sammy and explained in my latest reply to my own comment.
If the language doesn't have visibility features any method you use to try and protect the properties will simply be an ugly jerry-rig (that includes the prefix).
Even though _firstname does not change the property doing something like:
p[s_firstname] = "Nana";
does, which totally defeats the purpose of it
Good tutorial! Keep it up
Thank you for the feedback
Cool!