12
tahnik
8y

I never understood why some developers _ before their Javascript variable or functions. Do they serve any purposes? Other they just make everything look bizarre

Comments
  • 4
  • 2
    Mostly 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".
  • 2
    @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...
  • 0
    @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.
  • 1
    There 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.
  • 1
    Personally 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.
  • 0
    BMC llpp
    .
    thankl

    y km llkp km mk km mkll
  • 0
    .
    I
    MNM

    llllo
    m.om
  • 0
    lo
    kmmm.mnumkm.ib.m..b
  • 0
  • 0
    m mk lulk LM hi. thanks for pmmm
    mk. k

    mk

    .ll
    jm
    on4care j
    oi k
  • 1
Add Comment