Interesting Javascript Tips

Despite being a Javascript user for years, there were some things I still didn’t know.
Here are some of the interesting points I found from Ben Cherry’s presentation

names are always hoisted to the top of their scope
and always initialized to undefined

var foo = 1;
(function () {
alert(foo); // undefined
var foo = 2;
alert(foo); // 1

var foo = 1;
(function () {
alert(foo); // alerts "1"
// in the previous example foo is hoisted to the top as "undefined"
alert(foo); // 1

CSS expressions are evaluated right-to-left!

#foo div a {/* ... */}

this starts by looking at every
then it looks for those whose parent is

then it checks to see if its parent is #foo

parseInt needs radix!

parseInt("123"); // 123
parseInt("10"); // 10
parseInt("010"); // 8 -> WTF?

// with a radix
parseInt("010", 10); // 10 -> crisis averted!

never forget your radix

avoid inline scripts
these cannot be cached or minified
only put dynamic values here

you can detach sub-trees

var elem = $("#myelem"),
parent = elem.parent();

// ... muck with elem and sub-elements ...

.detach() is new in jQuery 1.4

how to make BC.util?

BC.util = (function (BC) {
var util = {},
foo; // private variable

// public
util.someFunction = function () {};

// private
function someOther() {}

return util;


// slow method
var i;
for (i = 0; i < someArray.length; i += 1) {
// ...
// faster
var i, l;
for (i = 0, l = someArray.length; i < l; i += 1) {
// ...

Popularity: 4% [?]

Learning Flex – Day 3

Today was the first day I made a bit of progress that I feel good about. I got some of the basic concepts involved with input elements, binding, and event handling. A lot of the concepts are similar to the DOM mechanism but some are deeper.

Of particular interest is the Event handling which has three types: Traversing?, capturing?, bubbling. Playing with the Debugging perspective gives you a better idea of how this works. The Current Target element and the actual Target element requires a bit of understanding and playing with the Debugger helps give you a clearer picture. Today was more learning than hands-on development.

For the longest time, I was playing around with an example I started on a while back and couldn’t figure out why the mx:Text field did not let me customize the background color and wouldn’t show up once the SWF was published. It turned out that I needed to use mx:TextInput and not mx:Text . Lesson learned!

While the GUI elements are necessary, I am not a big fan of having to re-arrange the elements since I tend to use copy-paste and since I was working in a haphazard order, the elements were all over the place. Flex development really requires some planning (especially the GUI) and will definitely help cut down the development time.

I am really waiting to get to the charts/graphs section so I can work on some Mint-like charts. Interesting, Terrence Ryan has provided the code to his Flex version of CFBuilder Stats on GitHub and the MXML code is above my head right now. It uses non mx: tags so I guess I have quite a bit more to learn.

Popularity: 2% [?]

Learning Flex – Day 2

After installing Flex on my VMWare Windows XP, I still could not connect to the ColdFusion server on my Mac and ended up installing ColdFusion on XP (although this was not the approach I wanted to take). My XP’s limited resources would probably get fully consumed by CF + FlexBuilder and also I wouldn’t be able to work on the CF files with Coda (it is possible but not the ideal way) so I looked around some more and found this post that showed how to fix the “Could not contact RDS server” issue. Editing the XML file solved my connection issue and once that was solved, I proceeded with Ben Forta’s example of building a Flex powered application that uses Coldfusion.

Once that was done, I continued with the example application that Ray presented at InsideRIA using ORM. This was interesting and it was interesting to see how the ORM works but that subject is worthy of a blog category of its own. Until I play around with it more and figure out how easy/hard it is to manipulate the data with finer control, I remain a skeptic.

By now, I have figured out how to get the Flex/CF to work on my Mac and I can return to using the FlexBuilder on the Mac (sans the Coldfusion/Flex builder wizard). Thank goodness, because the build time on the VMWare Windows XP was taking a while.

Popularity: 2% [?]

Learning Flex – Day 1

Today begins my attempt to learn Flex (after months of planning).

To start off – I began with a rather simple looking example by Ray Camden.

Unfortunately, I started out on my Mac and hit a wall. Ben Forta’s example uses the ColdFusion wizard which wasn’t available on the Mac platform yet (take that Steve Jobs!) and so I had to download and try it out on my VMWare Windows XP. Since it’s taking so long, I have to abandon Day 1. #Fail

Popularity: 1% [?]