Skip to content

5 more things you should know about JavaScript

25 September, 2010

Did you know that mankind knows more about the Moon than it does about the JavaScript language, FACT!

Here are some of the things recently discovered by a small scrum team exploring the jungles of Papua New Guinea.

1) void 0 === undefined. The undefined keyword is settable, thus undefined could equal true if you work with mischievous developers. void 0 provides a nice short way to test a variable.

var undefined;
(void 0 === undefined) // true
(typeof undefined === 'undefined') //true

2) The Range or TextRange object allows the manipulation of selected text on the page. Hmm, select, right click, create something with this text. Cool beans. Here’s more info on Range.

3) Ever tried maintaining focus in an Ajax application? document.activeElement will return the element you have focus on. If DOM is constantly being replaced then caching the active element is a good way to figure out where focus should be.

4) You really should know this one. You can use the or ‘||’ operand to declare default values as well as the normal conditional tests.

function myFunc(arg1){
  arg1 = arg1 || window.globalArg1|| 'default value';
}

5) Simple regular expressions are not expensive and can provide a nice terse way to write your code. Why not use it to check CSS class names for example.

// <body class="classname1 class name one">
/ class /.test(" "+document.body.classname+" ");
About these ads
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: