1

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!

4
  • 1
    You haven't shown us how you are calling the functions, and you haven't shown us how input is defined. Commented Apr 29, 2011 at 18:30
  • 1
    javascript is case sensitive i.e. myclient and myClient are different names. Commented Apr 29, 2011 at 18:30
  • Are you calling getClient() before you call dispatchClient()? This works fine: jsfiddle.net/f7xbK. Commented Apr 29, 2011 at 18:31
  • Sorry for the typo. its myClient only. Commented Apr 29, 2011 at 18:32

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

If 'input' is destroyed after 'getClient' function ends, would myClient still be valid?
If 'input.name' is String or Number, then yes
Thanks, what if it is an object that has sub properties?
Yes, because you're setting the name prop, which I assume is a string and not an object. If you were assigning the whole input object to myClient, or if input.name is an object then it would be a problem because objects are passed by reference.
Agreed, can't think of a reason this wouldn't work unless dispatchClient is called first. If these functions are attached as event handlers then it's entirely possible they are being called in the wrong order. If this is the case, then do a check for a null myClient value in dispatch, and if it is null, call getClient to set it up before doing anything else
|
0

myclient and myClient are two different variables.

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

1 Comment

Sorry, not me. Up voted to correct against my spelling mistake
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.