What are the use cases for closures/callback functions in JavaScript?

后端 未结 3 1342
栀梦
栀梦 2020-12-22 21:21

I was listening to Crockford\'s talk on JavaScript closures and am convinced of the benefit of information hiding, but I do not have a firm understanding of when to use call

3条回答
  •  星月不相逢
    2020-12-22 22:04

    Firstly:

    • Callback: A function passed as an argument to another function, usually to be called as a result of an event occurring.
    • Closure: A retained scope. I.e. the concept that when you declare a function within another function, the outer function's scope is accessible within the inner function.

    Callbacks can also be closures but are not always.

    This is a callback:

    someProcess(myCallback);
    
    function myCallback() {
        alert('Done...');
    }
    
    function someProcess(callback) {
        // does stuff...
        // ...
        callback();
    }
    

    A closure:

    function foo(msg) {
    
        function bar() {
            // I can access foo's scope
            // (i.e. bar can access everything that foo can access)
            alert(msg);
        }
    
        return bar;
    
    }
    
    foo('hello')(); // alerts "hello"
    

    One common usage of closures is to provide information-hiding, which is helpful in bringing some kind of encapsulation to the language. Have a look at the module pattern to see this in action.

    Another common usage is when the binding event handlers to elements. E.g.

    var myElements = [ /* DOM Collection */ ];
    
    for (var i = 0; i < 100; ++i) {
        myElements[i].onclick = function() {
            alert( 'You clicked on: ' + i );
        };
    }
    

    That wouldn't work. By the time the element is clicked, the variable i is 99. To make this work properly we could use a closure to capture the value of i:

    function getHandler(n) {
        return function() {
            alert( 'You clicked on: ' + n );
        };
    }
    
    for (var i = 0; i < 100; ++i) {
        myElements[i].onclick = getHandler(i);
    }
    

提交回复
热议问题