It's not really needed in JavaScript but I remember implementing something similar in C for school.
It's not really needed in JavaScript but I remember implementing something similar in C for school.
"JSON" is a way to serialize data to in a string. It's based on Javascript's object literal syntax.
JSON has absolutely nothing to do with this article.
What you mean is the memory locations are stored in an Object (or if you want to be specific about the language "a Javascript Object)
If you want to refer to Javascript's handy way of creating objects like so: var x = {};
That is an "object literal" (which also has little to do with the article)
I'd also note that there is something wrong with this as they could get the same behavior (only faster, smaller, and cross-browser) by just using an Array.
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.
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".
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.
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.