Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
tytho23148yMostly because JavaScript doesn't have private properties and methods, so underscore is the way one developer says to another, "Pay no attention to those, and don't use them, because I can change them without marking it as a breaking change".
-
@tytho which is exactly why JS needs private things so people don't have to use a hack like this. </rant>
No, I know a lot of people really like JS. Just not for me, I guess... -
tytho23148y@SecondThread agreed, JavaScript has so many quirks it makes me want to scream sometimes. Private variables is a thing I've wanted for a while, but I've since stepped away from the Object Oriented way of doing JavaScript, and it's not really a concern anymore.
-
chrizzle7268yThere are loads of module patterns out there that help to address this. E.g. http://adequatelygood.com/JavaScrip...
Typescript (or equivalent) can also do most of the heavy lifting in precompiling simplified code into fully fledged modules.
Not saying you should or shouldn't use the above but it helps me. -
brod99258yPersonally I prefix `_` when a variable should not be used for purposes other than formatting, sanitisation or validation..
For example a text input which is used to tag a post, which will be used as an array of values:
var myArray = [1, 2, 3] // usable value
var _myArray = "4" // text input, needs to be pushed to the array
Might sound confusing but most people get it right away when reviewing.
I never understood why some developers _ before their Javascript variable or functions. Do they serve any purposes? Other they just make everything look bizarre
undefined
javascript
underscore