user656925
user656925

Reputation:

How to avoid global variables?

I've been told not to use global variables.

I use one to turn off/on client side validation.

I use two more to conrol me drop-down menu.

If these should not be gloval variables where should I put them?

/* Global */

var client_validation=1,  // used to turn off/on client-side validation
    menu_timer=0,
    menu_elment=0;
window.onload=i0;

/*menu*/

function top_mouse_over(id)
  {
  bottom_mouse_over();
  if(menu_element)menu_element.style.visibility='hidden';
  menu_element=document.getElementById(id);
  menu_element.style.visibility='visible';
  }
function internal_time()
  {
  if(menu_element)menu_element.style.visibility='hidden';
  }
function mouse_out()
  {
  menu_timer=window.setTimeout(internal_time, 500);
  }
function bottom_mouse_over()
  {
  if(menu_timer)
    {
    window.clearTimeout(menu_timer);
    menu_timer=0;
    }
  }



Header 1  // This makes more sense
  Content

Header 1  // Than this
Content

Upvotes: 2

Views: 524

Answers (4)

Mike Thomsen
Mike Thomsen

Reputation: 37506

It sounds to me like you those might be good candidates for globals. Globals are supposed to, well, be reserved for operations with broad implications for the app like controlling fundamental behavior across multiple pieces.

Now, if you want to create a "namespace" for them, it's this simple:

var globalControls = {
    client_validation:1,
    menu_timer:0,
    menu_elment:0,
    window_onload:i0
};

Javascript doesn't have real namespaces yet. Objects are used as a substitute for them until the next version of ECMAScript might get around to adding that feature.

Upvotes: 2

Leblanc Meneses
Leblanc Meneses

Reputation: 3091

namespacing separates class definitions from colliding, however, it will not solve the global variable problem. It is a temporary fix.

A solution to global variables is to use a class instance which encapsulates properties that would otherwise be global. Encapsulation is one of the 3 pillars of object oriented programming.

See for an example of a class with a namespace that has encapsulated properties. Additionally, I show how to create a custom jquery plugin that uses the class. http://www.robusthaven.com/blog/javascript-development/Automatically-generate-the-table-of-contents-with-jQuery

When i have data that i need persisted that would normally require creating a global variable I instead use http://api.jquery.com/jQuery.data/ to attach the data in an object form to the dom element making use of this data.

Upvotes: 0

The title should be "How do I avoid global variables"? But that's ok.

You can create an object and assign properties to it. Then you'll have variables inside an object and these variables will only be accessible through this one, not through the global scope.

Example:

var config = {
    clientValidation: true,
    menuTimer: 0,
    menuElement: 0,
    someFunction: function () {
        // alert (this.clientValidation) "this" is the object scope
    }
};
// then you access the object properties:
alert( config.clientValidation ); // true

Upvotes: 0

Caimen
Caimen

Reputation: 2619

Here is an article about javascript namespacing.

http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/

Upvotes: 1

Related Questions