Hacker News new | ask | show | jobs
by lelandfe 483 days ago
As another borrowed convention, JS developers of yore (and likely some today still) used an _ prefix to denote “private” function/methods. Quotes as it’s just a convention - today JS supports # for actual private members in class syntax.
3 comments

This is a direct descendant of ActionScript. It introduced the convention of Instance._getter and Instance.__private.
That looks like ActionScript 2, so the javascript convention was actually well in place by then. I can't speak confidently about the late 90s, but I wouldn't be surprised if it predated Actionscript 1 as well.

But doesn't it all trace back to C conventions anyways?

Not at all.

AS1 was plain ES3.

movieclip._x or movieclip._alpha (as in position and alpha blending) were accessed by properties with underscore to denote that you were using accessors (getters/setters).

Internally properties like Object.prototype.__proto__ had two underscores to denote they were private.

In AS2 you had:

  class Foo extends Bar implements Xyz {
    private foo
    public function set x (value : Number) : void { ... }
    public function get x () : Number { }
AS2 was based on long forgotten ES4 specification [0]. Under the hood it was still, what we would call today, transpiled to ES3 bytecode, as it run basically on the same VM as AS1.

It's crazy to think it took us 10 years to reinvent the wheel with TypeScript.

AS3 was a complete rework, more akin to Java.

[0] https://evertpot.com/ecmascript-4-the-missing-version/

None of what you say here that is true moves the needle on what you say that isn't true.

Programmers using underscore to denote identifiers for internal use was well-established and in use long before ActionScript even appeared. To claim that it's a "direct descendant" of ActionScript is arbitrary, anachronistic, and odd.

What about Lingo?
Another weird case: In Google's server-side JS environment Apps Script, function names that end with _ are treated as private functions that cannot be called directly by the user or enumerated.

1. https://developers.google.com/apps-script/guides/html/commun...

In the Python world I was in, trailing underscores is used to work around the ban on reserved words. The language grabs some of the best names for itself!

So a variable that really ought to be named `class` you name `class_`.

In java usually clazz
That's what I used to do before discovering this.
I use underscore to prevent shadowing of variables names. Not sure where I picked up that habit though.
I've seen that before.

A related conventional use of underscores in JS variable names is when "discarding" values in positional settings, like destructuring arrays.

E.g. `const [_, a, b] = triple`

In general, underscores in JS seem to be used to help call out what to ignore or what is less important.

You've been able to `const [, a, b] = tuple;` for a wee while now FYI.

But yeah for every other time most linters will accept _ as "ignore this".

I had no idea that was a thing!

Thanks for teaching me something today.

Oh yeah, I always forget about that.