回調函數

請解釋什麼叫作回調函數並提供一個簡單的例子數組

回調函數是一個函數,它被做爲參數傳入另外一個函數,當某些操做結束後,該函數被調用。下面是一個簡單的例子,當數組被修改後,調用回調函數打印一行日誌。bash

function modifyArray(arr, callback) {
  // do something to arr here
  arr.push(100);
  // then execute the callback function that was passed
  callback();
}

var arr = [1, 2, 3, 4, 5];
modifyArray(arr, function() {
  console.log("array has been modified", arr);
});
複製代碼
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息