Dakusan's Domain Forum

Main Site Discussion => Posts => Topic started by: Dakusan on November 22, 2010, 03:53:05 am

Title: Something I feel JavaScript really got right
Post by: Dakusan on November 22, 2010, 03:53:05 am
Original post for Something I feel JavaScript really got right can be found at https://www.castledragmire.com/Posts/Something_I_feel_JavaScript_really_got_right.
Originally posted on: 11/21/10

One thing I always really miss when working in other dynamic languages that aren’t JavaScript is the ability to access known (non dynamic) members of an associative array/object/hash (called a hash from here on out) through just a single dot. This matches C’s syntax of accessing struct members, as opposed to being forced into using array syntax which is harder to read IMO in languages like PHP and Perl. For example...


Creating a hash in:
JavaScript:var Hash={foo:1, bar:2};
Perl:my %Hash=(foo=>1, bar=>2);
PHP:$Hash=Array('foo'=>1, 'bar'=>2);

Accessing a Hash’s member:
JavaScript:Hash.foo or Hash['foo']
Perl:$Hash{foo};
PHP:$Hash['foo']

The reason this is preferable to me is it can make code like the following
Zones[Info['Zone']]['DateInfo'][Info['Date']]['UniqueEnters']+=Info['Count'];
much more readable by turning it into the following
Zones[Info.Zone].DateInfo[Info.Date].UniqueEnters+=Info.Count;