How is a closure different from a callback?

前端 未结 9 960
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 19:08

I asked a question about callbacks and arrived at another question (see comment). How is a closure different from a callback?

相关标签:
9条回答
  • 2020-12-07 19:35

    What is a callback function?

    A callback function is a function which is:

    • passed as an argument to another function
    • is invoked(लागू) after some kind of event
    • once its parent function completes, the function passed as an argument is then called

    in Plain English we say A callback is any function that is called by another function, which takes the first function as a parameter or function passed as an argument

    • Note : invoked : The code inside a function is executed when the function is invoked. or we say like this It is common to use the term "call a function" instead of "invoke a function".

    It is also common to say "call upon a function", "start a function", or "execute a function".

     function getUserInput(firstName, lastName, age, callback2,callback1) {
        var fullName = firstName + " " + lastName;
    
        // Make sure the callback is a function
        if (typeof callback2 === "function") {
        // Execute the callback function and pass the parameters to it
        callback2(fullName, age);
        }
    	
        if (typeof callback1 === "function") {     
        callback1(lastName);
        }
    }
    
    function callbackforlastname1(lname){
     console.log("---");
    }
     
    function genericPoemMaker(name, aged) {
        console.log(name + " is finer than fine wine.");
         console.log("A " + aged + " of unfortunl smile");
    }
    
    getUserInput("Avinash", "Maurya", "26", genericPoemMaker,callbackforlastname1); 
    ऐसे कॉल करते है

    0 讨论(0)
  • 2020-12-07 19:38

    A callback depending on a context variable aka bound variables (== object state) will be a closure. It will be a pure function, otherwise, when it only takes free variables (== parameters).

    0 讨论(0)
  • 2020-12-07 19:45

    Here is a way to differentiate between those two:

    Closure

    A Closure is used to extend functionality, for instance if a user clicks a button, we want something to happen on the screen, in that case, we would use a Closure where we pass the user event (a click) and then push data to the view.

    Callback

    A callback is more or less similar to a closure, but it is more used to inform and provide synchronous capabilities. For instance if you perform jQuery Ajax calls, you'll have callbacks such as success(), error(), beforeSend() and so forth to handle the asynchronous data.

    0 讨论(0)
  • 2020-12-07 19:47

    I fail to see how the two are even related? A closure carries parts of a local state into a function of some sort, think of it as passing by reference.

    A callback is meant to notify you about certain change and it redirects program flow. The closure could modify local state but you would never get processor time to handle that, like you would with a callback.

    0 讨论(0)
  • 2020-12-07 19:48

    Different definitions:

    Callback -

    a callback is executable code that is passed as an argument to other code.

    Closure -

    a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables.

    0 讨论(0)
  • 2020-12-07 19:49

    In simple words: a callback using context variables is a closure.

    0 讨论(0)
提交回复
热议问题