什么是回調(diào)函數(shù)?
維基百科定義:
a callback is a piece of executable code that is passed as an argument to other code which is expected to call back (execute) the argument at some convenient time
翻譯:回調(diào)函數(shù)就是作為另外一個函數(shù)的參數(shù),但這個參數(shù)是一串可執(zhí)行的代碼,另外一個函數(shù)在合適的時間執(zhí)行這個參數(shù);
function fn (callback)
{
callback("value");
};
function bb(sth)
{
alert(sth);
}
fn(bb);
這里bb就是回調(diào)函數(shù),它作為fn的一個參數(shù),這個參數(shù)就是bb函數(shù)的方法體,然后在fn執(zhí)行過程中,執(zhí)行bb();并傳入"value";
回調(diào)函數(shù)是同步還是異步?
回調(diào)函數(shù)只是一種實現(xiàn)方式;如出門可以做公交,開車等,都可以到達目的地;所以回調(diào)函數(shù)和同步/異步?jīng)]有直接的關(guān)系,它既可以同步,也可以異步;