News for hackers. Stuff that matters.
Re:Fun with JavaScript Arrays by SandB0x on Wednesday June 12, 2013 @06:32AM (#5867398)
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.
Re:Fun with JavaScript Arrays by jdlshore on Wednesday June 12, 2013 @08:11AM (#5867635)
Yes, 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.)

Re:Fun with JavaScript Arrays by gambler on Wednesday June 12, 2013 @10:54AM (#5868375)
Yes, 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.

Re:Fun with JavaScript Arrays by cristiantincu on Wednesday June 12, 2013 @12:10PM (#5868822)
The `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/Refe...

Re:Fun with JavaScript Arrays by teh_klev on Wednesday June 12, 2013 @12:15PM (#5868858)
I 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)

Re:Fun with JavaScript Arrays by ludwigvan on Wednesday June 12, 2013 @09:45AM (#5867998)
> 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.

Re:Fun with JavaScript Arrays by cema on Wednesday June 12, 2013 @09:53AM (#5868047)
Not 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.
Re:Fun with JavaScript Arrays by jdlshore on Wednesday June 12, 2013 @10:21AM (#5868195)
Technically, 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 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. :-)
Re:Fun with JavaScript Arrays by cema on Wednesday June 12, 2013 @04:41PM (#5870585)
Yes, 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'}