Call (877) SITE-OPT (748-3678)

Code: Remap objects

Chapter 8 - Ajax Optimization

The bulkiness of JavaScript code, beyond long user variable names, comes from the use of built-in objects such as Window, Document, Navigator, and so on. For example, given code such as this:


you could rewrite it as this:


Commonly, we see people perform remaps of frequently used methods such as document.getElementById( ) like this:

function $(x){return document.getElementById(x)}

Given the chance for name collision, if you decide to employ such a technique, we would suggest a slight variation, such as this:

function $id(x){return document.getElementById(x)};
function $name(x){return document.getElementsByName(x)}
function $tag(x){return document.getElementsByTagName(x)}

and so on.

Object and method remapping is quite valuable when the remapped items are used repeatedly, which they generally are.

Copyright © 2002-2018 Website Optimization, LLC. All Rights Reserved - Free website speed test - Privacy Policy
Last modified: February 19, 2009.

Follow us on: Twitter, Google+, Facebook, Linked In