top | item 43157994

(no title)

ldjb | 1 year ago

I was expecting this post to explain why it's an underscore specifically, as opposed to a dollar sign, an asterisk, a caret, a tilde or some other special character.

I can only assume it's a holdover of languages like C where the standard library has some reserved names that start with an underscore.

https://devblogs.microsoft.com/oldnewthing/20230109-00/?p=10...

discuss

order

lelandfe|1 year 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.

zeroq|1 year ago

This is a direct descendant of ActionScript. It introduced the convention of Instance._getter and Instance.__private.

phist_mcgee|1 year ago

I use underscore to prevent shadowing of variables names. Not sure where I picked up that habit though.

layer8|1 year ago

I wouldn’t say it’s a “holdover”, it continues to be a common convention for marking internal or reserved use.

paradox460|1 year ago

Some modern languages even turn _ flagged variables/parameters into black holes

anoldperson|1 year ago

I do it every day, so I see it from your perspective.

I'm willing to bet there are kids who do not though. Given how many crazy colours my IDE shows everything in, I could definitely believe there are people that go, 'Why bother, aren't those private members blue anyway?', or some such similar train of thought.

Of course if your IDE is little more than notepad, then such things are still important. For me, that's the Arduino IDE. I have to admit I really like writing micro controller code in it, it's a bit like writing code 30 years ago (both the good and bad parts).

kazinator|1 year ago

Making it a dollar sign would cause countless bugs for people who generate HTML using templating languages whose variables are indicated by dollar sign. From time to time, someone would add target="$blank" to a templates, forgetting that the $ must be escaped to be literal.

They might have to resort to predefining a reserved variable called blank, whose value is "$blank". :)

Similar reasoning applies to most other special characters.

Given how HTML gets generated by preprocessors which use special characters in this manner or that, its best not to come up with new schemes within HTML itself involving special characters.

Carving out a reserved space within an existing namespace is safe.

robocat|1 year ago

Your reason is unlikely to be a cause.

The _ comes from the W3C in 1995 well before JavaScript was commonly used for templating HTML.

Scripting has used $ for variables for a long time: I think the most relevant history line for $variable is PHP comes from Perl comes from shell scripts. I also remember finding $ ugly on Vax.

There were a huge variety of templating syntaxes for server side and HTML generation was virtually all server side in the 1900s.

Server side languages were very rarely JavaScript before Node in 2009.

JavaScript wasn't used much for HTML generation before Ajax. There were soon after many many client side templating syntaxes.

I'm guessing only Brendan Eich could say why $ was accepted for JavaScript variable names.

Timelines are hard because the foundations were compressed within a decade: JavaScript 1995, PHP 1996, DHTML 1997, Ajax early 2000s, jQuery 2006.

Syntaxes tend to be extremely path dependent, and every developer cribs from everything they use.