2

I need help understanding the fundamental difference between a closure and the bind() function in Javascript. I have tried .bind for setting this scope and used closure to get the scope.

  1. What is the fundamental difference between the bind() function and a closure in JS?
  2. In which scenario should the bind() funciton be used and in which scenario should a closure be used?

Here is the code. I'm using ul as a closure in JS. Can I do this using the bind() function?

ul.find("li").each(function(a, ele) {
    $(ele).attr("tabindex", options.items[a].tabindex);
    $(ele).on("focusout", function () {
        setTimeout(function (ul) {
            $activeElement = $(document.activeElement);
            //ul.find("li").indexOf($activeElement) < 0 ? $(ul).focus() :"";
            debugger;
        }(ul), 1000);
    });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Shan Khan
  • 9,667
  • 17
  • 61
  • 111
  • Define "closure". What is your definition of a closure? – Ram Jun 28 '16 at 07:04
  • 1
    The `bind()` method and a closure are very different concepts, so I'm unsure what comparison you're expecting. This sounds like an X/Y question. What is the problem you're actually trying to solve? – Rory McCrossan Jun 28 '16 at 07:05
  • i have updated the question. cant the above code can be done using function bind – Shan Khan Jun 28 '16 at 07:07
  • And once again a closure-related question that doesn't have any closures. – Ram Jun 28 '16 at 07:09
  • `ul` should be third parameter passed to `setTimeout` instead of within `()` following anonymous function, though note, `setTimeout` will be called without `1000` delay between calls within `.each()` using current `js`. – guest271314 Jun 28 '16 at 07:09
  • 1
    Firstly, you don't have a closure. You have an IIFE. Secondly, you can avoid the problem entirely by not using `each()` and applying a single event handler to all the `li` elements. – Rory McCrossan Jun 28 '16 at 07:09
  • my old question answered as set closure in timeout. here you guys saysing its not closure :( confused ! http://stackoverflow.com/questions/38052714/how-to-access-variable-from-closure-scope-in-javasript – Shan Khan Jun 28 '16 at 07:13
  • @ShanKhan What is expected result? – guest271314 Jun 28 '16 at 07:14
  • @guest271314 - i want to retrive the UL inside the timeout and by adding a paramters, it works. My question was to get the basic difference between function bind and closure as the other question link shows to add closure in timeout – Shan Khan Jun 28 '16 at 07:15
  • 3
    The accepted answer of your previous question doesn't make sense and adds unnecessary overheads to the code. The `ul` variable had been defined in the outer scope and you can easily use it in the `setTimeout` scope, no need to use other code for accessing the variable. – Ram Jun 28 '16 at 07:17
  • @Vohuman yes i have changed that code in this question. also how we can say its IIFE as its not wrapped like this (function(){})() - its the simple timeout used. ? – Shan Khan Jun 28 '16 at 07:20
  • Use third parameter of `setTimeout` to pass variables `setTimeout(function (_ul) { // do stuff with _ul }, 1000, ul); // remove "()" following function(){}` . `.bind()` does not appear to be necessary at `js` at Question, though neither does passing `ul` as parameter as `ul` variable should be accessible within `setTimeout` – guest271314 Jun 28 '16 at 07:21
  • Have a look at http://stackoverflow.com/q/8928880/1048572, http://stackoverflow.com/q/33513945/1048572, http://stackoverflow.com/q/34359738/1048572 to see how completely different they are – Bergi Jun 28 '16 at 08:14
  • Possible duplicate of [Closure or Bind](https://stackoverflow.com/questions/34359738/closure-or-bind) – Shan Khan Sep 25 '17 at 17:20

0 Answers0