0

I hope this isn't "opinion-based" but what does an underscore before a function denote?

e.g. _someFunction(){...}.

I'll use an underscore for partial files in scss, like _buttons.scss or _layout.scss... but I also see it a lot in JavaScript and not quite sure what it means, other than it seems to be for semantics only, vs. changing the functionality.

Kirk Ross
  • 3,907
  • 5
  • 39
  • 67

3 Answers3

2

Typically it's a marker for indicating that a function is to be considered 'private'. Private can mean a few different things depending on context, such as:

  • This function should not be called outside of the file.
  • This method of the class should only be called by other methods of this class.

It also typically means that as a user of the library, you should not rely on these methods to not change between minor versions. They are not a public API.

Furthermore, some tooling will pick up on the underscore and not consider it when for example automatically generating documentation.

This is 100% convention, so nothing prevents you from actually using this.

Evert
  • 75,014
  • 17
  • 95
  • 156
0

In terms of javascript it means nothing.

In terms of javascript programming culture it signifies a private function/method.

Javascript does not have private binding. All methods in an object are public (at least up to ES6). But there are times where programmers need to tell other programmers not to use specific functions. That those functions are only meant for internal use and won't be maintained in the future.

The underscore notation was kind of adopted from Perl culture since Perl is also a language that does not have private methods.

slebetman
  • 93,070
  • 18
  • 116
  • 145
0

Its just a way to denote that its a private function and hence will be used in js in that script only and can't be used in html

kaps_rocks
  • 18
  • 5