Your problem is in the way you're declaring functions:
var funcB = function(){
// function codes
funcC();
}
var funcC = function(){
// function codes
funcB();
}
When you're declaring funcB
, it tries to find funcC
, which doesn't exist, yet. This breaks your code, throwing an undefined
error.
You can define the functions like this:
function funcB(){
// function codes
funcC();
}
function funcC(){
// function codes
funcB();
}
However, if funcB
calls funcC
, and funcC
calls funcB
, it doesn't matter how they're declared / initialized, you're going to have an infinite loop, probably resulting in a stack overflow-like error, or a crashing browser (/tab).
Chrome throws this:
RangeError: Maximum call stack size exceeded
To avoid that error, do something like this:
function funcB(calledFromC){
if(!calledFromC){
funcC(true);
}
}
function funcC(calledFromB){
if(!calledFromB){
funcB(true);
}
}
Or shorter:
function funcC(calledFromB){
!calledFromB && funcB(true);
}