Kiran
Kiran

Reputation: 5526

fundamental javascript question

I declared a global:

var myClient;

I have a function:

//Contact is a record object that has toString() which prints the name.
function getClient() {
   myClient = new Object();
   debug(input.contact); // This prints properly
   myClient.contact = input.contact;
   debug(myClient.contact); // This prints properly
}

I have another function that is trying to use the same:

function dispatchClient() {
    debug(myClient.contact);
}

And the result I see is undefined. Is something wrong here? (Ignoring the design aspect)

If that is wrong, then how can I pass the state of global between functions? If that is not wrong, then hmm, I may need to dig deeper!

Upvotes: 1

Views: 139

Answers (4)

ShaneBlake
ShaneBlake

Reputation: 11096

Your var is myclient while your using the object later as myClient. Javascript is case sensitive, so these arn't the same variable.

Upvotes: 0

Vismari
Vismari

Reputation: 745

If you are calling getClient() before dispatchClient() *myClient* variable would be undefined.

Other possiblity is the name(case sensitive), in dispatchClient() you are using my*C*lient that is diferent than the global variable my*c*lient.

If possible(depend on your code), create a instance at the beginning:

var myclient = new Object();

Upvotes: 0

Headshota
Headshota

Reputation: 21449

myclient and myClient are two different variables.

but code must still work. unless you call dispatchClient before getClient function.

Upvotes: 0

Imran
Imran

Reputation: 91149

dispatchClient is probably being called before getClient gets called. At that point, myClient would be still undefined.

Upvotes: 3

Related Questions