开发者

Organising top level functions in a Javascript file

Occasionaly we will have a page that required some page specific开发者_StackOverflow社区 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


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.


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() {

   }
}


See namespaces

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜