Dot vs. Square Bracket Notation in Javascript

I have been looking for an explanation on this for a while. This article summed it right up, Javascript Square Bracket Notation

Javascript also offers an alternative property accessor notation using square brackets in a way that is similar to the way in which the indexed members of an Array are accessed, except that a string is used between the square brackets. This alternative notation is extremely powerful and useful but gets very little coverage in most books on javascript and that seems to leave novice javascript programmers unaware that they even have this option.

Using square bracket notation you can change this:


into this:


More importantly though, you can use variables as function names:

var genericString = 'Hello, my name is Ryan.';

var stringMethod = 'split';

genericString[stringMethod](' ');

// returns the array ["Hello,", "my", "name", "is", "Ryan."]

Write a TextMate Command to Order your CSS Properties

I like to have my css property lists ordered by string length (smallest to largest). Some my find it fussy but I feel they are easier to read when formatted this way. Looking at this stuff all day makes my eyes pretty tired and I’ll do whatever I can to improve things.

TextMate has a great feature called Commands. These are essentially small programs you can write using common programming languages (I prefer php since it’s the only one I know). You can invoke these commands using key shortcuts or tab triggers.

Since I spend a lot of time formatting my css, I wanted to make it an automated process that I could fire off using the key shortcut option+return.

Be careful not to use an existing key shortcut when choosing your own. TextMate is smart enough to not overwrite the previous shortcut but instead shows you a contextual menu of the choices bound to the shortcut. This slows down the workflow, adding more steps to get the end result.

Start by opening a new document and select Bundle Editor > Show Bundle Editor


With the Bundle Editor open, select New Command. This will create a new command in whatever bundle was currently selected.

I prefer to keep all my customizations in my own bundle. This makes it easy to keep the bundles current between my home and work machines. It also makes it easy to share with friends. To create your own bundle simply select the “New Bundle” menu item first. Then add a “New Command” to it.


Give the new command a descriptive title.


Next, select what input you’d like. This is what gets passed to your command from the document. Choose “Selected Text” with a fallback of “Scope”. For the output, choose “Replace Selected Text”. You can also use “Show as Tool Tip” while debugging. This simply pops up a yellow tool tip showing you the result.


TextMate tracks the location your cursor to determines it’s scope. For instance, if you’re in-between the enclosing braces in a css rule, TextMate will determine the scope as You can check the scope of the current cursor location using the key shortcut shift+control+p.

We want to edit the order of a css property list so we’ll need to use as our scope. This way, TextMate will pass us all the properties inside the enclosing braces, including the braces.

We also need to choose our activation method. We want a “Key Equivalent” for this so click inside the input field and press the key shortcut you’d like to use. The input field will record your actions and show the shortcut using the associated key icons.


To start using a specific language in your command you need to let TextMate know where to look. #!/usr/bin/php is the default path to the php executable on a mac running Snow Leopard.

Open the php declaration with <?php and start coding.

Make sure there is no empty lines between the php path and opening declaration. If there is, this will get returned to your document in the form of an unwanted newline character. This drove me batty until a helpful person replied to my question on the TextMate mailing list.


Here’s the tricky part, getting the input parsed out from the STDIN (standard input) constant. I referred to this excellent explanation for help. To see other data to which you have access, use print_r($_ENV). This shows an array of goodies you can use in in your command.

[updated 12.13.09]Allan Odgaard suggested using file_get_contents("php://stdin") instead of $fstat = fstat(STDIN); $stdin = fread(STDIN, $fstat['size']); The updated command can be downloaded using the same download link at the end of the article.


Here is the full commented code for the command.


// Get the properties list
$stdin = file_get_contents("php://stdin");

// remove the enclosing brackets
$pattern = '/\{(.*)\}/Us';
preg_match($pattern, $stdin, $matches);

// Trim off the whitespace
$match = trim($matches[1], "\n");

// Turn each line into a member of an array
$match = preg_split('/\n/', $match);

// Check the string length of each member.
function sort_strlen($val_1, $val_2) {
       // initialize the return value to zero
       $retVal = 0;

       // compare lengths
       $firstVal = strlen($val_1);
       $secondVal = strlen($val_2);

       if($firstVal > $secondVal) {
               $retVal = 1;
       } else if($firstVal < $secondVal) {
               $retVal = -1;

       return $retVal;

// Iterate through the array using the preceding function to organize the array.
uasort($match, "sort_strlen");

// Wrap the finished product in braces.
$output = '{' . "\n";
foreach($match as $line) {
	$output .= $line . "\n";
$output .= '}';


Now that I have added my command and if my cursor is located inside a properties list, I can invoke it using option+return.

Here is what my properties looks like before the command:


And after:


Download the command.

Unzip and double click to install it.

Tip: Invert Your Screen Colors for Easier Reading

In terms of web design, light text on a dark back ground can give some dynamic results. When it comes to reading on the web though, I prefer the reverse. What happens, especially when the scenario is white text on a black background, is that similar to looking into a bright light, the text can become “burned” into my vision very briefly. As I end a line of text and start to wrap back, the ghost text stays with me and takes a sec to fade away. Needless to say, it can make reading very unpleasant.

There are a few simple tricks I use to alleviate this issue.

  1. Write my own stylesheet for the browser with simple defaults to make the background and text reversed. Not my first choice as I usually want to experience the intended design first. Find out more about user stylesheets.
  2. Use the excellent Readabilty bookmarklet. I use this on most news sites to remove all the blinking ads. It’s not prefect though. It has trouble saving the inline images and doesn’t work with paged articles, which so many sites use to gain more ad revenue.
  3. Or the simple shortcut (on Mac only) of Control + Option + Command + 8. While it seems long it’s actually pretty simple. This command can be toggled quickly and is therefore my favorite.

Example: Perishable Press, a well designed and excellent source of WordPress and general web development tips.

Before the key command:

Normal view

After the key command:

Inverted view

And if you forget the key command you can always visits your Mac’s system preferences and make the change.

Preferences dialog

While this makes reading easier on the web, I actually use light text on a dark background when I’m coding. Seems counter intuitive but I’ve found it preferable. I need to think a bit more about why it makes such a difference…

Example using TextMate‘s Blackboard theme (modified):

TextMate Blackboard theme

Use Good Tools

I was looking for something in the TextMate menu when I noticed the “Daily Tips” item. I enabled it and received the following tip. Waaay geeky but I’m a huge fan.

textmate tip

Copyright © Ryan Fitzer 2009. All rights reserved.

RSS Feed. This blog is proudly powered by Wordpress and uses a modified version of Modern Clix, a theme by Rodrigo Galindez.