javascript 为什么 Underscore.js 有延迟功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16890106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Underscore.js have a delay function?
提问by Aadit M Shah
This is the source code for Underscore.js' delay
function:
这是 Underscore.jsdelay
函数的源代码:
_.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
?
这与 有什么不同setTimeout
?为什么 Underscore.js 需要delay
?
回答by alex
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.
这是一种能够传递额外参数的跨浏览器方式,这些参数将显示为回调的参数,例如setTimeout()
. 这在 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.
我同意它是Naomi 的回答中概述的不太有用的 Underscore 方法之一。
回答by Thank you
Why does Underscore.js have a delay function?
为什么 Underscore.js 有延迟功能?
Because dumb. This particular underscore.js method seems pretty stupid.
因为笨。这个特殊的 underscore.js 方法看起来很愚蠢。
Cons
缺点
- additional function in lib means larger code base
- larger code base means more to maintain and more possible bugs
- code that uses this function now has a dependency on that lib
- lesser/nil improvement over native API means low cost:gain ratio
- new apis to learn
- lib 中的附加功能意味着更大的代码库
- 更大的代码库意味着更多的维护和更多可能的错误
- 使用此函数的代码现在依赖于该库
- 对原生 API 的较少/零改进意味着低成本:收益比
- 要学习的新 API
Pros
优点
this section intentionally left blank
本节故意留空
I would just learn to use javascript and do something like
我只会学习使用 javascript 并做一些类似的事情
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.
很简单吧?Underscore.js 有时非常无用。老实说,window.setTimeout
就这样非常有用。
Here's another example to show how to pass an arg to the function
这是另一个示例,用于展示如何将 arg 传递给函数
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
如果你不能依赖.bind
你也可以使用闭包
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 !
哇,虽然这很困难。我要回到下划线、lodash 和 jquery。这个 JavaScript 的东西很难!
回答by terrance.a.snyder
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.
那个图书馆的作者,他选择用业余时间开源它,谈论它,用它作为一种向人们介绍 javascript 的方式,也许让某人在闭包范围内有一个灯泡时刻,认为有了这个,图书馆的吸引力就增强了。
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.
_.delay, _.defer, _.throttle, _.after
有一个流恕我直言,比窗口更好地阅读。
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.
除此之外,通常我也喜欢编写节点服务器端(nodejs),而不必切换/进入/退出模式……尝试window.timeout
在节点中使用,看看会发生什么。
回答by Plynx
Not much, although it fits thematically with defer
, debounce
, and so on. This means you can use the underscore wrapping notation:
不多,尽管它在主题上与defer
、debounce
、 等等相符。这意味着您可以使用下划线包装符号:
_(yourfunction).delay(1000);
Also it doesn't seem like it will let you get away with a string argument, which would call eval.
此外,它似乎不会让您摆脱字符串参数,该参数将调用 eval。