- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Wait, isn't the first one just a non-terminated recursive method eating up stack till it runs out? I'm curious, because I never attempted to overload in JavaScript, I didn't even know that a redefinition doesn't result in an error.
Admin
One can have sort-of-overloading by type-testing on the duck-typed arguments - but it's not a great idea.
Admin
It is, but it never gets called because:
Indeed. That's ... disappointing, but not entirely unsurprising. It's a bit like all the fun you can have in Python if you carelessly assign a new value to
self.frodo
whenself.frodo
exists and is (in theory) a function. The function object disappears and whatever you assigned takes its place.Luckily:
But also:
So Python does it, too.
Admin
Just as i said yesterday that we have a leading candidate for upper management, competition emerged the very next day.
Admin
The greasy pole has lotsa wannabe's scrabbling at the bottom. And more every day.
Admin
That's pretty much every day in WTFLand.
Admin
I doesn't default unpassed in arguments to null, it defaults them to undefined. A similar value, but still distinct.
null !== undefined
,typeof undefined === "undefined"
, andtypeof null === "object"
are all true in JS.Admin
That the parser accepts it in the first place is heresy.
Like, in nested scopes why not. But declaring 2 functions with the same identifier in the same scope? The language shouldn't allow that. JS is TRWTF.
Admin
JS is definitely very WTFy, but there isn't much choice - that's the language in all the browsers.
TRWTF is using JS on the server, or using languages like PHP - there are much better choices in that arena.
Admin
Actually it defaults them to
undefined
. But that's also falsey, so the logic is the same.Admin
That senior dev might be thinking of TypeScript, which on the surface does have operator overloading... but only until you compile it down to JavaScript, at which point yeah, it's gone.
Also, any number of tools would've caught that (ESLint, IDE's). Which makes one wonder what editor that senior dev was using.
Admin
Now that's a silly question.
Real dev's use Notepad or vi depending on the server OS in use. Running directly on the production server where all edits are made live.
Admin
Fortunately, you don't have to worry about getting a different default value because someone has changed the value of "undefined"... right? right?
Admin
Javascript in this case feels like IBM 370 Assembly in the 70s. My colleagues used to produce code that modified code, e.g. change a goto to a no op. This was before MMU I guess.
Admin
It didn't. The second definition replaced the first definition. If you wish to revise your argument to "It allowed the running code to change the definition of an existing function? The language shouldn't allow that.", then you're on to something. However, javascript's define-as-you-go behavior was necessary for what its original purpose was. The real WTF is that this language was chosen as the base for modern professional platforms.
Admin
I'm soooo happy that Dart, which has transpiling to Javascript as a mandatory goal, did not expose this insanity at the Dart level. You have type bool, with values true and false, as a required type for everything that needs a conditional. You have class Null with a single instance null. And "undefined" does not exist.
Admin
FTFY: The real WTF is that this language was chosen as the base for any platforms.
Admin
There are times where the difference between null (deliberately not there) and undefined (just... not there...) are useful. Those times are few and far between, and 99% of the time, null and undefined are handled the same way by your code. (And when you get to JSON, the typical data interchange format, 'undefined' doesn't exist, all is null, and that's well and good, so the weirdness in JS doesn't leak elsewhere.)
But typeof null === 'object' is godawful. It's even worse when, once upon a time, I had to deal with a JS dialect that didn't throw any kind of exception when you tried to access undefined.undefined.undefined, it just gave you null -- so when I was traversing down a tall object and doing a quick "this last thing exists, right?" check, it would say that yes, I definitely found an object at the bottom of this -- even if I'd typo'd something and was actually holding a null.
Admin
One case where null vs. undefined is useful is for something like a generic update function. So something like
function update(obj, newName) { // if newName is undefined, keep the current obj.name // if newName is null, clear obj.name by setting to null if (newName !== undefined) { obj.name = newName; } }
Note that in JSON, it is also possible to have null vs. undefined:
{ "newName": null } vs. {}
However, depending on the JSON library and the target programming language, getting the different between these two JSONs may not be easy.