函数调用栈

--定义

函数可以调用其他函数,这个函数又可以调用其他函数。栈用来存储函数之间的调用顺序当函数被调用,这个函数会被加到调用栈的顶部;当函数执行结束,这个函数会被从调用栈中移除。

--说明

以JavaScript 代码为例,说明函数调用函数时,栈的应用。

function first(){
 console.log('Executing first function');
 console.log('Calling second function');

 second();
}

function second(){
 console.log('Executing second function');
 console.log('Calling third function');
 third();
}

function third(){
console.log('Executing third function');
}

first();

如上,为当third被调用时栈的状态。

third(); 执行完毕后,third()从栈顶移除,此时栈顶是second;second()执行完毕后,从栈顶移除,此时栈顶是first;直到first(); 执行完之后从栈顶移除,first() 运行完毕。

原文地址:https://www.cnblogs.com/luffystory/p/9193584.html