Hacker News new | ask | show | jobs
by anc2020 5518 days ago
I appreciate your enthusiasm for correctness, but if I'm reading correctly, the essence of sktrdie's comment was trying to counter the group think that the idea of the article is in some way "bad", "satirical", or "wrong".

It may be that I only ever merit to use this pattern once every 10,000 lines, but that in no way makes the idea of calling with reference over primitive values incorrect. Readers should understand the difference between rare and never.

1 comments

> he essence of sktrdie's comment

Sure, however the first part of my comment is not intended to address the essence of their comment, so it has no bearing. What they said is incorrect, and in fact a common mistake and I wanted to correct it.

However, I'd also note that what sktrdie is describing is an Array (in Javascript an Array is just an object with numbers for property names and some helper methods).

As far as I can see there is absolutely zero benefit (and many drawbacks) in using the technique outline in the OP article as opposed to just using an Array.

You could get the same result by simply using an Array directly...or if you want to get fancy, add a few simple helper methods.

In this way (and perhaps others) the article is "bad" and "wrong".

Okay, great, you're absolutely right that an Array would have been a better choice of data structure, albeit marginally better.

That was not my qualm, and neither does it seem to be the qualm of other commenters on this page. Of course, I'm having to second-guess the reasons of other commenters because they were disinclined to note them, but am simply contesting the notion that the article is satirically bad.

> marginally better.

I'm not so sure.

As written it's wasteful of resources and not cross-browser. (also not entirely functional)

All the stuff with the closure and defineProperty is basically pointless....

In the end what they have is an object with numbers for properties, and they are sticking values in those properties. That's an array.

All the hoops they jump through don't do anything except heat up the processor and waste resources, with nothing in return for that.

Consider this code which does the same thing:

  var $ = function(val) {
      var addr = $.mem.length;
      $.mem[ addr ] = val;
      return addr;
  }
  $.mem = [];

  var foo = "foo";
   foo = $( foo );
   console.log( foo )  // 0
   console.log( $.mem[foo] ) // "foo"
This code accomplishes the same thing but is much faster, leaner, simpler, and cross-browser.

I'd also say that all of this doesn't really do much to inform one about "pointers". Learning about the link between pointers and arrays in certain other languages might be helpful for some JS developers, but there's none of that here. Here is just a roundabout array implementation that doesn't really teach the reader anything.

I'm not going to spit on the efforts of someone who is trying to learn something, or be condescending about it (at least not intentionally)....but I think it's necessary to point this sort of thing out so everyone walks away the wiser.