javascript - How does the load() function allow the user to provide a callback? -


in javascript it's popular libraries/frameworks let define callback function post-processing of data.

eg.

load("5", function(element) {     alert(element.name); }); 

i wonder how load() function looks able let user provide callback?

are there tutorials this?

well, load function this:

function load(arg, callback) {    var element = { name: "foo " + arg }; // pass    if (typeof callback == 'function') {     callback(element);   } } 

with typeof check make sure callback argument object can invoke, function.

then example:

load("5", function(element) {     alert(element.name); // show `"foo 5"`. }); 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -