473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

passing private variables

The answer probably is staring me in the face and I don't see it, so
I'll just ask.

I try to limit the scope of variables and avoid global variables. But
sometimes I get stumped by the following situation and need some help:

A function sets the value of a variable that is limited to the scope of
the function. Later, another function needs the variable but does not
need to call the first function and trigger everything else in that
function. What's the best practice way of getting at that variable
without making it global?

Jul 23 '05 #1
3 1372
On 7 Dec 2004 06:55:12 -0800, jeffg <je*********@fuse.net> wrote:

[snip]
I try to limit the scope of variables and avoid global variables.
Excellent! :D

[snip]
A function sets the value of a variable that is limited to the scope of
the function. Later, another function needs the variable but does not
need to call the first function and trigger everything else in that
function. What's the best practice way of getting at that variable
without making it global?


It may depend on your actual code. A URL would be nice. That said, the
general approach would be to have the two functions share the same
"private" scope. For example:

/* Global variables that will soon hold
* references to your functions.
*/
var myA, myB;

/* Define and call an anonymous function. When this occurs,
* a new scope will be created where you can place your
* common, local variables.
*/
(function() {
var myCommon;

/* These inner functions for a closure which keep the
* local variable, myCommon, in memory once the anonymous
* function returns.
*/
myA = function() {
/* ... */
};
myB = function() {
/* ... */
};
/* Note the call here... */
})();

Both of the functions, myA and myB, can now access myCommon. The pattern
is similar if myA and myB were methods of an object.

Take a look at <URL:http://www.crockford.com/#javascript> and
<URL:http://www.jibbering.com/faq/faq_notes/closures.html> for articles
that delve into this sort of thing more deeply.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
VK
JavaScript doesn't have a fine-grained scope settings (like main - local -
my in Perl, public - protected - private in Java etc.)
Actually, if you need to keep the variable value between function calls,
there is nothing non-academical (if you are concerned about that) in making
such variable global.

If you cannot determine in advance how many global variable to declare, you
can use an object as a "variable holder". I would suggest do not trash up
the window, just make a new holder with a unusual name (to do not override
it by mistake):

var $_glob = new Object();
....
$_glob[variableName] = variableValue;
....
someFunction($_glob[variableName]);
....


Jul 23 '05 #3
On Tue, 7 Dec 2004 16:23:14 +0100, VK <sc**********@yahoo.com> wrote:
JavaScript doesn't have a fine-grained scope settings (like main - local
- my in Perl, public - protected - private in Java etc.)
Not through the use of keywords, no, but private and public is still
simple to achieve. Protected is more tricky, though. See my other post and
the links there.
Actually, if you need to keep the variable value between function calls,
there is nothing non-academical (if you are concerned about that) in
making such variable global.
If that code is going to be reused, I disagree. Library-like code should
be completely self-contained and expose as little as necessary to make
sure clashes don't occur.
If you cannot determine in advance how many global variable to declare,
you can use an object as a "variable holder".


That's effectively the same thing. The only difference is that the
variable/activation object of a function is used, rather than a property
of the global object.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Prosonman | last post by:
Hi, What is the best way to transfer variables to and from a dialog box? My project consists of a form with a number of controls, lets say three Labels, when a label is clicked it opens a dialog...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
11
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
1
by: Eric | last post by:
Hello, I am trying to come up with the best way to pass large amounts of data from page to page, namely a data table. The user needs to enter data into a form in one page and confirm it on...
3
by: IntraRELY | last post by:
I have the following function, Notice how I am passing the dateInterval as a string. What is the correct way to pass "DateInterval.Year" as a variable to a function? TIA, Steve Wofford...
7
by: John Layton | last post by:
Hi there, Is it possible to pass null to a function taking an "out" (or "ref") parameter in C#. I'd like to do something like the following (which doesn't compile of course). Thanks in advance....
7
by: The Doctor | last post by:
A rather elementary question, In VB5, how can I pass a variable from one form to another?
8
by: chimambo | last post by:
Hi All, I am trying to pass variables between two frames in PHP. I want to get a variable from F1 to F2 and the variable that is now in F2 should pass back the variable to F1 and call another...
6
by: gggram2000 | last post by:
I'd like to know how I go about passing two variables from one form to the next. For example, I can pass a variable like this: This demonstrates how to pass a variable to a form when the form is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.