Chapter 22

Advanced Techniques

WHAT’S IN THIS CHAPTER?

  • Using advanced functions
  • Tamper-proofing your objects
  • Yielding with timers

JavaScript is an incredibly flexible language that can be used in a variety of styles. Typically, JavaScript is used in either a procedural manner or an object-oriented one. The language, however, is capable of much more intricate and interesting patterns because of its dynamic nature. These techniques make use of ECMAScript language features, BOM extensions, and DOM functionality to achieve powerful results.

ADVANCED FUNCTIONS

Functions are one of the most interesting parts of JavaScript. They can be quite simple and procedural in nature, or they can be quite complex and dynamic. Additional functionality can be achieved through the use of closures. Furthermore, function pointers are very easy to work with, since all functions are objects. All of this makes JavaScript functions both interesting and powerful. The following sections outline some of the advanced ways that functions can be used in JavaScript.

Safe Type Detection

JavaScript’s built-in type detection mechanisms aren’t foolproof and, in fact, can sometimes give both false positives and false negatives. The typeof operator, for example, has several quirks that can make it unreliable in detecting certain types of data. Since Safari (through version 4) returns "function" when typeof is applied to a regular expression, it’s difficult to definitively determine if a value is a function.

The

Get Professional: JavaScript® for Web Developers, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.