Why does Underscore.js have a delay function?

2020-08-09 09:13发布

问题:

This is the source code for Underscore.js' delay function:

_.delay = function (func, wait) {
    var args = slice.call(arguments, 2);
    return setTimeout(function () { return func.apply(null, args); }, wait);
};

How is this any different from setTimeout? Why does Underscore.js need delay?

回答1:

It's a cross browser way of being able to pass extra arguments which will appear as the arguments to the callback, like setTimeout(). This doesn't work in IE.

It can make your code prettier...

setTimeout(_.bind(function() { }, null, "arg1"), 1e3);

...vs...

_.delay(function() { }, 1e3, "arg1");

I agree that it's one of the less useful Underscore methods, which are outlined in Naomi's answer.



回答2:

Why does Underscore.js have a delay function?

Because dumb. This particular underscore.js method seems pretty stupid.

Cons

  1. additional function in lib means larger code base
  2. larger code base means more to maintain and more possible bugs
  3. code that uses this function now has a dependency on that lib
  4. lesser/nil improvement over native API means low cost:gain ratio
  5. new apis to learn

Pros

this section intentionally left blank


I would just learn to use javascript and do something like

var hello = function() {
  console.log("hello");
};

var delay = 1000;

window.setTimeout(hello, delay);

Simple, right? Underscore.js is sometimes pretty useless. Honestly, window.setTimeout is perfectly useful just the way it is.


Here's another example to show how to pass an arg to the function

var Cat = function(name) {
  function meow(message) {
    console.log(name, "says meow!", message);
  }
  this.meow = meow;
};

var duchess = new Cat("Duchess");

window.setTimeout(duchess.meow.bind(duchess, "please feed me!"), 2000);

// 2 seconds later
// => Duchess says meow! please feed me!

If you can't depend on .bind you can use a closure too

window.setTimeout(function() {
  duchess.meow("please feed me!");
}, 1000);

Wow, that was difficult, though. I'm going back to underscore and lodash and jquery. This JavaScript stuff is tough !



回答3:

aes·thet·ics

The author of that library, who chose to spend his/her spare time to open source it, talk about it, use it as a way to introduce people to javascript and perhaps get someone to have a light-bulb moment around closure scope thought the library's attractiveness was enhanced by having this.

Arguing the relevancy of this function in isolation is like arguing the relevancy of a painting or other piece of craftsmanship. Some may like it, others may not.

You are free to like it or not. I, personally, prefer just-get-to-the-point libs.

_.delay, _.defer, _.throttle, _.after have a flow IMHO that reads better than window.

On-top of this, generally I also like writing node server side (nodejs) and not have to shift/in/out of mode... try using window.timeout in node and see what happens.



回答4:

Not much, although it fits thematically with defer, debounce, and so on. This means you can use the underscore wrapping notation:

_(yourfunction).delay(1000);

Also it doesn't seem like it will let you get away with a string argument, which would call eval.