0

I need some concept of threading java script.Actually I struck in one problem .Problem is that I have one function A () calling function B and C.

function A(){
   B();
   C();
}

function B(){
   //doing some task 
   i=something;
   alert(i);
}

function C(){
   // i need value I here.
   alert(i)    //getting undefined 
}

I need to synchronised call ...

3
  • Define i as a global variable. Commented Jul 17, 2013 at 4:51
  • 2
    All those calls are synchronized. In other words, the call to B() will be called and completed, before the call to C(). So I'm not sure why you're talking about Threading, when it looks like you're talking about Scope. Commented Jul 17, 2013 at 5:04
  • please check this question stackoverflow.com/questions/17689556/… Commented Jul 17, 2013 at 5:12

3 Answers 3

5

How about

function A(){
   C(B());
}

function B(){
   //doing some task 
   var i=something;
   return i;
}

function C(i){
   // i need value I here.
   alert(i)    
}

or split out for readability

function A(){
   var resultFromB = B(); //
   C(resultFromB);
}

function B(){
   //doing some task 
   var result=something;
   return result; // return it to calling function
}

function C(resultOriginallyFromB) { // passing it
   alert(resultOriginallyFromB);    
}
Sign up to request clarification or add additional context in comments.

4 Comments

+1 for avoiding globals.
not quite clear that B() is returning something to be passed along later, but it does save on memory by not re-declaring a variable.
@Brombomb you mean I should have var returnedFromB=B(); in C?
@mplungjan I think it would help with readability, and be a simpler form of code. This may help the author fully grasp what you are doing so it can be applied later and not fall into some kind of nesting hell.
1

Set i as global like,

var i=null;// global i
function A(){
   B();
   C();
}

function B(){
   //doing some task 
   i=something;
   alert(i);
}

function C(){
   i need value I here.
   alert(i)    //getting undefined 
}

Read this also

Alternatively, you can use return in B() like,

function A(){
   i=B();
   C(i);//passing i in C()
}

function B(){
   //doing some task 
   i=something;
   alert(i);
   return i;//return i
}

function C(i){
   // value i has been passed in.
   alert(i);
}

2 Comments

I'll give you the benefit of the doubt
Ok, I also removed the comment.
0

Actually it should not really be alerting undefined in function C. As i would have become globally defined already, without the use of var.

Besides try doing it this way, so that you don't clutter the global space:

(function() {

  var i;

  function A(){
   B();
   C();
  }

  function B(){
     //doing some task 
     i=4;       // --- or something
     alert(i);  // --- will alert 4
  }

  function C(){
     // i need value I here.
     alert(i)    // --- will alert 4
  }

  A();  // --- Assuming you were calling 'A' earlier as well

})();  

And yes, nothing related to threading here.

3 Comments

Have a look there, now.
please check again this question .stackoverflow.com/questions/17689556/…

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.