4 ms·
Bear in mind that I know very little JavaScript so I may be completely wrong or this may be a very simple observation, but isn't the confusion in the first part
by SandB0x 13y ago
Bear in mind that I know very little JavaScript so I may be completely wrong or this may be a very simple observation, but isn't the confusion in the first part simply due to the printed representation of the Array prototype/object?
I seem to remember that arrays in JavaScript are in theory just objects/dicts such that:
var a = [1, 2, 3];
is equivalent to
var a = {0 : 1, 1: 2, 2: 3};
and whatever function is invoked by evaluating `a` in the console must just iterate over the sorted keys of the object, starting at key = 0 and attempting to print key + 1 until the key does not exist. So he may be setting `a[-1]` and it happily exists, but the confusion is just down to the whatever the equivalent of the `__repr__` function is for JavaScript arrays.
- jdlshore 13y agoYes, most of the confusion is the author's misunderstanding of how arrays work in JS, combined with his tool not showing everything he put into the array. You're correct that arrays are objects, which means they consist key/value pairs. Keys must be strings; values can be anything. So when you say `a[0] = 3;`, JS converts that to `a["0"] = 3;` and stores `{ "0": 3 }`. So all those "funny" examples the author provides are just variants on converting an expression to a string. For example, `a[typeof a] = 4` is equivalent to `a["object"] = 4` (or `a.object = 4`) because `typeof a` is "object". There's one wrinkle on this, though: objects and arrays aren't exactly the same thing. Arrays are a special kind of object, as far as JS is concerned. JS maintains a special "length" property for arrays. It doesn't do that for other objects. (The same applies to JS functions and Regexes. They're objects, but they're special kinds of objects that can do things regular objects can't.)
- ludwigvan 13y ago> JS maintains a special "length" property for arrays. It doesn't do that for other objects. (The same applies to JS functions and Regexes. They're objects, but they're special kinds of objects that can do things regular objects can't.) Indeed JS functions have a special `length' property too, where one can check the # of arguments specified in the function definition.
- cema 13y agoNot just the length property, but also a special array API (push, pop, etc). So there is no equivalence between an array and the simple object constructed from its keys and values, but the object would correctly represent a snapshot of the subset of array key-value pairs.
- jdlshore 13y agoTechnically, the special API is because arrays extend Array.prototype. You could create an object that does the same, and push(), pop(), etc would work.* Try going to http://www.objectplayground.com http://www.objectplayground.com and putting in the following code, selecting the "Show built-in objects" option, and evaluating: this.array = []; this.object = Object.create(Array.prototype); this.object.push("foo"); this.object.push("bar"); *Well, they sort of work. They use and modify the "length" property, so you could see some weird behavior under some circumstances. Not recommended for production use. :-)
- cema 13y agoYes, but a literal will still be either an array or an object, so that var arr = ['a','b'] will be different from var obj = {0:'a',1:'b'}
- gambler 13y agoYes, most of the confusion is the author's misunderstanding of how arrays work in JS, combined with his tool not showing everything he put into the array. There is nothing in the post that shows that author doesn't know how arrays are represented internally. Moreover, whether or not he does is largely irrelevant. He shows clear examples of bad language design, and they remain such whether you're aware of the inner workings of the language or not. JavaScript's arrays have issues that span far beyond mere "string representation". For example, you can't safely use for...in construct to iterate over items in an array.
- cristiantincu 13y agoThe `for … in` statement iterates over the enumerable properties of an object¹. It’s not for arrays. 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- teh_klev 13y agoI think you missed this bit at the beginning of the article: "Also keep in mind that sometimes I'm playing stupid, but sometimes I'm actually stupid." I suspect the author knows exactly how arrays work in JS and is just mucking about to demonstrate one of the "Bad Parts". This is pretty much in the same vein as the WAT video (https://www.destroyallsoftware.com/talks/wat https://www.destroyallsoftware.com/talks/wat)