Javascript (using jQuery) in a large Project... organization, passing data, private method, etc.

Posted by gaoshan88 on Stack Overflow See other posts from Stack Overflow or by gaoshan88
Published on 2010-03-08T06:10:11Z Indexed on 2010/03/08 6:21 UTC
Read the original article Hit count: 208

Filed under:
|

I'm working on a large project that is organized like so: Multiple javascript files are included as needed and most code is wrapped in anonymous functions...

// wutang.js

//Included Files Go Here

// Public stuff
var MethodMan;

// Private stuff
(function() {

var someVar1;

MethodMan = function(){...};

var APrivateMethod = function(){...};

$(function(){
//jquery page load stuff here
    $('#meh').click(APrivateMethod);
});
})();

I'm wondering about a few things here. Assuming that there are a number of these files included on a page, what has access to what and what is the best way to pass data between the files?

For example, I assume that MethodMan() can be accessed by anything on any included page. It is public, yes?

var someVar1 is only accessible to the methods inside that particular anonymous function and nowhere else, yes? Also true of var APrivateMethod(), yes?

What if I want APrivateMethod() to make something available to some other method in a different anonymous wrapped method in a different included page. What's the best way to pass data between these private, anonymous functions on different included pages? Do I simply have to make whatever I want to use between them public? How about if I want to minimize global variables?

What about the following:

var PootyTang = function(){
    someVar1 = $('#someid').text();
    //some stuff
};

and in another included file used by that same page I have:

var TangyPoot = function(){
    someVar1 = $('#someid').text();
    //some completely different stuff
};

What's the best way to share the value of someVar1 across these anonymous (they are wrapped as the first example) functions?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery