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 Answers 4
dispatchClient is probably being called before getClient gets called. At that point, myClient would be still undefined.
6 Comments
myclient and myClient are two different variables.
but code must still work. unless you call dispatchClient before getClient function.
1 Comment
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
Your var is myclient while your using the object later as myClient. Javascript is case sensitive, so these arn't the same variable.
inputis defined.myclientandmyClientare different names.getClient()before you calldispatchClient()? This works fine: jsfiddle.net/f7xbK.