Passing arguments in anonymous functions in JavaScript

后端 未结 5 1117
遥遥无期
遥遥无期 2021-02-02 11:29

Sometimes I see JavaScript that is written with an argument provided that already has a set value or is an object with methods. Take this jQuery example for instance:



        
5条回答
  •  醉话见心
    2021-02-02 11:53

    If you looked at the code for createServer, it'd look something like this:

    function createServer (handleRequestAndResponseFunction) {
        handleRequestAndResponseFunction(actualRequest, actualReponse);
    }
    

    ok, no it wouldn't, but it's a simple example. createServer takes a function that accepts two arguments.

    In more realistic terms, when you pass in that function of two arguments, it does whatever middleware processing and stuff that it needs to, and then calls that function, passing its own request and response variables.

提交回复
热议问题