jquery - When to use closures in javascript? -


i came across custom filter used in angularjs returning function logic custom filter. here i'm not able understand when should using closures. tried return function in jquery call function, control doesn't go inside function body, control goes inside angularjs custom filter. can me understand concept.

angularjs custom filter code, control goes inside anonymous function:

app.filter('myfilter', function () {     return function (curitem, txtsearch) {         var results = [];         if (txtsearch && curitem) {             (i = 0; < curitem.length; i++) {                 // logic filter             }             return results;              app.filter('myfilter', function () {                 return function (curitem, txtsearch) {                     var results = [];                     if (txtsearch && curitem) {                         (i = 0; < curitem.length; i++) {                             alert(curitem[i].name);                         }                         return results;                     }                 }             });}}}); 

jquery function i'm trying pass control in return anonymous function although know able in call instead of using closure.

$('div').on('click', function () {     return function () {         alert('');     } }); 

i suggest should first understand closures before trying use them. recommend reading you-dont-know-js book on github getting clarity on topic. neatly , conceptually explained in this chapter.

after understanding closures , advantages, can identify situations apply it.

hope helps!


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -