3 ms·
Let bindings? Huh?
by robin_bb 19y ago
Let bindings? Huh?
- nostrademons 19y agoUsually done using: (function(var1, var2) { // var1 and var2 are now bound within this block })(val1, val2); Why use this instead of... var var1 = val1; var var2 = val2; // do stuff with var1 & var2 It's because if you have any closures within the block, they'll capture the bindings of var1 and var2 instead of their values. That means that if you're inside a loop, your bindings will be overwritten each time through, so you could end up creating a dozen closures that all do the same thing. Here's an example from a JQuery patch I did a couple weeks ago: http://dev.jquery.com/attachment/ticket/1737/bug1737.patch http://dev.jquery.com/attachment/ticket/1737/bug1737.patch Before it used an eval statement to directly write the value into the source code of the closure. After, it captures the value with a let-binding and then returns a closure over the current value instead of the current binding.
- robin_bb 19y agoThanks. I thought that someone was suggesting that they were using let = ... syntax. Now, I understand that the idiom you showed is "let binding" in the Javascript world.