Javascript function callback example

In Javascript, function callback is to call the function that was passed in as a parameter. For example, there are functions A and B, B is passed into A as a parameter, then in the body of function A, it calls function B. In jQuery, when you do
$(document).ready(function(){/* do something here */});
The jQuery ready() is function A, and the anonymous function you passed into the ready() is function B.

The functionality of the code snippet below is the same as the jQuery’s document ready function, but it is highly simplified for function callback demonstration purpose.

// Declaring a singleton object cp
var cp = new function()
	this.docready = function(aCallbackFunction)
		document.addEventListener("DOMContentLoaded", function() {
    		aCallbackFunction();// Call the function that is passed in as a parameter
// Call the singleton object cp's docready function, passing the anonymous function as a parameter
	var p = document.createElement("p");
	var txtNode = document.createTextNode("Document Object Model(the current html page) is fully loaded and parsed.");


Search within Codexpedia

Custom Search

Search the entire web

Custom Search