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){
// ...
}
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
loadVideosfunction needed some data for the request to be made, you’d havegetDatareturn the value(s) needed, which would then be used. Or ifparamsis anObject, you could just have it populate theObjectwithout needing to return it.