styler
styler

Reputation: 16501

passing function parameters from one function to another, is this code acceptable?

Just trying to get an understanding of what I'm allowed to do in javascript. This question is about functions and whether or not what I've got in the following snippet is good practice or what way would you do this?

JS

function func1(arg){
  // run code ...
  func2(arg);    
}

function func2(arg){
  // run code ...
  func3(arg);    
}

function func3(arg){
  alert(arg);
}​

func1('my message');

So for instance if I had a function that displayed videos and required a parameter and inside this function I called a function that made an ajax call which also required a parameter then how would I pass that?

function loadVideos(param){

  //...

  getData(param);

}

function getData(param){

  // ...
}

Upvotes: 0

Views: 141

Answers (1)

I Hate Lazy
I Hate Lazy

Reputation: 48761

Yes, you can have functions that invoke other functions. I assume in your first example each function does something useful.

In your second example, you'd do it just like you have it. If you need to wait for the AJAX response to invoke the second function, you'd call it in the callback to the AJAX request.

If the loadVideos function needed some data for the request to be made, you'd have getData return the value(s) needed, which would then be used. Or if params is an Object, you could just have it populate the Object without needing to return it.

Upvotes: 1

Related Questions