views:

92

answers:

3

Occasionaly we will have a page that required some page specific Javascript functionality. I will create a javascript file called myPageName.js.

The question I have is how do people organise their top level functions. I currently have 15+ top level functions defined like this

function function1 (containerElement) {
    ...do function1;
}

function function2(slotActionButton) {
    ...do function2;
}

function function3(slotId) {
    ...do function3
}

This seems wrong and I am just polluting the global namespace. I am using jQuery. What are people's thoughts?

TIA

Pat Long

+3  A: 

I try to namespace everything I can!

Your top level functions can go in a namespace too right? Maybe something like :

var myApp = {};

myApp.function1 = function(containerElement){
...
}
myApp.function2 = function(containerElement){
...
}
.
.
.

etc.

Lewis
A: 

See namespaces

Upper Stage
I am not disagreeing with Lewis or Mickel but the Namespace link from Upper Stage had reference to the attaching to the prototype approach I was thinking of doing anyway
Pat Long - Munkii Yebee
+1  A: 

I like to use namespaces in all my projects, for example...

// Define namespace
var myNs = {};

myNs.web = {
   function1 = function(attr, attr1) {

   },
   function2 = function() {

   }
}

myNs.otherStuff = {
   function1 = function(attr, attr1) {

   },
   function2 = function() {

   }
}
Mickel