global variables

M

mr_burns

hi,

i was wondering if anybody could tell me when a function is called and
variables are defined within that function, are the variables global
where i can call and modify the variables in other functions.

for example:



function fdosomethingfirst() {

//this function will be called first

var v_num1, v_num2

v_num = 1234;

}

function fdosomethingagain() {

//if i were to do something in this function, could i use the
variables from the previous function???

}


if the function - fdosomethingfirst() - was called first, is it
possible to use the variables used in that in - fdosomethingagain()??
i would try this out but im at work now and really need to know later
on. much appreciated.

burnsy
 
D

Dennis Biletsky

No, if you declare variable in function it will be local. If you need global
declare variable not in function like this

var v_num1, v_num2

function fdosomethingfirst() {


v_num = 1234;

}

function fdosomethingagain() {


}
 
M

Michael Winter

[snip]
function fdosomethingfirst() {

var v_num1, v_num2

These are declared local in the function. Once the function exits, they
won't exist.
v_num = 1234;

This is a previously undeclared variable. Because it doesn't use the var
keyword, it is declared global.
}

function fdosomethingagain() {

//if i were to do something in this function, could i use the
//variables from the previous function???

You could use v_num as it's a global variable, but not v_num1 and v_num2.

There are a few ways around this.

1. Declare all shared variables global.

This is a simple approach, but it's the least attractive from both a
theoretical and practical point of view (no link between data and
behaviour, and data accessable by all which could lead to name clashes).


2. Pass the variables as parameters to the second function.

This should need no explanation.


3. Use an object.

You could create an object that has the variables v_num1 and v_num2 as
properties of the object, and the two functions as methods of the same
object.

There are a couple of ways to apply this.

- Create an object (either by constructor or literal):

var v = {
this.fdosomethingfirst = function() {
this.num1 = ??; // On first write to these variables, they
this.num2 = ??; // will be created as properties of the object
}
this.fdosomethingagain = function() {
// ...
}
}

Alternatively, you could make the variables private, where only the
methods of that object can access them:

var v = {
var num1, num2;

this.fdosomethingfirst = function() {
num1 = ??; // Don't use the 'this' operator this time
num2 = ??;
}
this.fdosomethingagain = function() {
// ...
}
}

This is an application of closures.


- Return an object from the first function which is passed to the second

function fdosomethingfirst() {
// ...
return { num1: <value>, num2: <value> };
}
function fdosomethingagain( obj ) {
var v_num1 = obj.num1;
var v_num2 = obj.num2;
}

var retObj = fdosomethingfirst();
fdosomethingagain( retObj );


Does that help in any way?

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,253
Latest member
BlytheFant

Latest Threads

Top