5 more things you should know about JavaScript
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+" ");