What value do we get by using closures in JavaScript for implementing callbacks and and event handler attachment . I know jQuery uses this pattern extensively but want to understand why ?
What value do we get by using closures in JavaScript for implementing callbacks and
Share
Assigning a closure as a callback gives you access to the scope which it is defined within. It also helps keep your namespace clean. Other libraries allow you to bind object methods as event handlers, giving the event access to the object, which is a pretty elegant alternative solution. For instance, maybe something like this:
Although my implementation also uses closures haha.