Computing stuff tied to the physical world

JavaScript semantics

In Software on Jan 24, 2013 at 00:01

Some things are quite surprising in JavaScript / CoffeeScript:

    $ coffee
    > '1'+2
    > '1'-2
    > 1 < null
    > 1 > null
    > 1 < undefined
    > 1 > undefined
    > a = [1,2,3]
    [ 1, 2, 3 ]
    > a.b = 4
    > (x for x in a)
    [ 1, 2, 3 ]
    > (x for x of a)
    [ '0', '1', '2', 'b' ]
    > (x for x in 'abc')
    [ 'a', 'b', 'c' ]
    > (x for x of 'abc')
    [ '0', '1', '2' ]

It makes sense once you know it … but that’s the whole thing with being a newbie, eh?

That array-with-poperties behaviour is actually very useful, because it lets you create collections which can be looped over, while still offering members in an object-like manner. Very Lua-ish. The same can be done with Object.defineProperty, but that’s more involved.

For the full story on “array-like” objects, see this detailed page. It gets real messy inside – but then again, so does any other language. As long as its easy to find answers, I’m happy. And with JavaScript / CoffeeScript, finding answers and suggestions on the web is trivial.

On another note: Redis is working well for storing data, but there is a slight impedance mismatch with JavaScript. Storing nested objects is trivial by using JSON, but then you lose the ability to let Redis do a lot of nifty sorting and extraction. For now, I’m getting good mileage with two hashes per collection: one for key-to-id lookup, one for id-based storage of each object in JSON format. But I’m really using only a tiny part of Redis this way.

  1. The one that gets me is that equality isn’t transitive.

    E.g., this is true: (0 == “”) && (“0” == 0) && (“0” != “”). Also, (NaN != NaN) && (NaN !== NaN) is pretty spooky too.

    Each comparison result sort of makes sense on its own but it’s all confusing enough that I made myself a little crib sheet:

  2. @redis: i’m putting my jeenode traffic data in couch db. So you query all properties using views.

  3. Check this video relative to javascript (and other language) strange things. It’s really hilarous (for coders)

  4. This is a nice JavaScript wat? with an educational and well-balanced explanation:

Comments are closed.