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

前端 未结 6 790
再見小時候
再見小時候 2020-12-30 13:27

In javascript it\'s very popular for libraries/frameworks to let us define a callback function for post-processing of data.

eg.

load(\"5\", function(         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 13:37

    In JavasScript functions are first-class objects. That pretty much means they act like other built in types. You can assign them to variables, pass them into functions, etc.

    This article is a helpful link explaining how functions as first-class objects work in JavaScript: http://helephant.com/2008/08/functions-are-first-class-objects-in-javascript/

    Joel Spolsky has a detailed and interesting explanation on some of the interesting things/ways you can use functions as first class objects in JavaScript: http://www.joelonsoftware.com/items/2006/08/01.html

    Finally, since they're first class objects, functions can very easily accept other functions as parameters:

    var load = function(callback) {
      // load something here
      callback();
    }
    

提交回复
热议问题