6 secrets of JavaScript Jedis

Are you a competent JavaScript developer but have yet to hit Jedi-level secrets? No worries -- these steps will take you to the next level

Page 3 of 3

JavaScript secret No. 5: Protecting the global scope

One of JavaScript's flaws is that scripts on a page are all executed in a shared global context. When exploited, this flaw can leave a website vulnerable to cross-site scripting attacks, for example. The shared global context can lead to other problems. For example, many scripts run on a page, and not all of them are always determined by you (such as ads). Those scripts run in the global space and can all access the same global variables. If two of those scripts happen to use the same global variable, they begin interfering with each other. The code then breaks.

Minimizing use of the global scope is a defensive technique that helps eliminate interference and save you hours of debugging time. Chances are you can't eliminate storing variables globally, but you can minimize your footprint by using techniques like namespacing:

// **Namespacing**

// The global footprint is a single object containing

// all other references needed.

var MyApp = {};

MyApp.id = 42;

MyApp.utils = {

validate: function(){

//do work



Or the module pattern:

// **Module pattern**

// This relies on immediately invoked anonymous functions and the

// closure they create to maintain access to variables within the

// function.

var MyApp = (function(){

//declare a local variable

var appId = 42;

function getAppId(){

//referring to a variable in this function's parent scope

return appId;


return {

getAppId: getAppId



appId; // undefined

MyApp.appId; //undefined

MyApp.getAppId(); // returns 42. (Brought to you by closures)

JavaScript secret No. 6: Inheritance

JavaScript has a long and confusing history with inheritance, for several reasons. Most developers -- including many JavaScript developers I've met -- have a solid understanding of the classical model and are utterly confused by the prototypal model. This makes lots of sense after you peruse a list of languages that use prototypal inheritance.

With the exception of JavaScript, prototypal inheritance is not part of any mainstream language. To make matters worse, it's possible to emulate the classical model in JavaScript. As a result, there are all sorts of approaches to inheritance, many of them opinionated and contradictory. I suggest avoiding the pseudo-classical approach entirely, as it's one of JavaScript's sirens. It looks familiar to most developers, but breaks down quickly because it's only an approximation.

There are just a few topics every expert JavaScript developer should know, and a high-level overview at that. Are any of these causing you pain? Have I left something out that deserves to be mentioned? Start the conversation in the comments.

This article, "6 secrets of JavaScript Jedis," was originally published at InfoWorld.com. Keep up on the latest news in application development and read more of Andrew Oliver's Strategic Developer blog at InfoWorld.com. For the latest business technology news, follow InfoWorld.com on Twitter.

| 1 2 3 Page 3