JavaScript tips and tricks (3)

Date Arrow  August 5, 2007

How to organize code in “namespaces”

When you use lots of libraries in your code, you can easily pick up a function name that corresponds to a pre-existing name in some library that you might have included. To avoid that, you should create namespaces that encapsulate the code of your application:


Then, you can start adding members (functions, classes, variables, etc) to that namespace:

The following image shows how Firebug displays the objects added in their own namespaces, and also how the Ext and the Yahoo! code appear in their own namespaces:

firebug.png

Create Objects and Arrays the Easy Way

To create objects and arrays in JavaScript, you can of course use the constructor+methods syntax:

While the above syntax is OK, many JavaScript interpreters can handle the following, completely 100% equivalent version, much faster and more “JavaScript-like” (by this I mean that you are more likely to find this in JavaScript libraries):

Create a Singleton Object

If you need to create a “singleton”, yet complex object, do not fall in the “classical” way of doing things: do not create a class and then instantiate just one instance!. Since JavaScript objects can be created on the fly, you can use object literals for that.

However, if you need to create just one object, with a complex structure, you can use the following trick:

The trick is in the line 11 of the example above:

Do you see the parentheses after the closing bracket and before the semicolon? Well, this triggers the execution of the function, which “returns” a literal object with methods and properties, and which can reference private members (since they are closures).

This pattern is very common in the Ext Framework!

Scheduling Function Execution

Adding a method to the “Function” class, you can schedule its execution a couple of milliseconds in the future, encapsulating the “Window.setTimeout()” method:

The Ext framework has a method that does exactly this, called “defer()”. Very handy!

Concatenating Strings

If you have to concatenate strings, avoid using the “+” operator whenever possible; the Array class can be used as a Java “StringBuffer” or a .NET “StringBuilder”, as follows:

The above code will display the following output on the web page:

a
long
array
of
strings
is
here

The use of the “join()” method is considered as slightly faster than using the “+” operator, which implies much more object copies in memory, which in turn triggers the garbage collector much more often.

Iterating over Arrays

When you operate on array objects, you usually end up writing code like this:

But things do not have to be that awful all the time:

Now you have code that’s much easier to read!

Using “toString()” for Reflection

Let’s suppose that you have the code written above

The last instruction above shows a laconic “[object Object]” which does not tell much about what’s inside your object… Actually, the “alert()” function, when applied to any JavaScript object, will call the “toString()” method to its parameter: so try adding a “toString()” to your objects instead:

With this code, you’ll get this output in a dialog box:

Easy Code Injection

This code allows you to inject arbitrary code around any function, like if you were doing some cheapo AOP:

Do not hesitate to submit your own snippets or tricks! I hope this is useful to you.

Similar Posts:

Tagged   Code · How to?

Commenting