Function

bind

_.bind(function, object, *arguments)

绑定函数到一个对象意味着每当调用该函数时,的值将是对象。或者,将参数传递给函数以预先填充它们,也称为部分应用程序。对于没有上下文绑定的部分应用程序,使用partial

var func = function(greeting){ return greeting + ': ' + this.name }; func = _.bind(func, {name: 'moe'}, 'hi' func( => 'hi: moe'

bindAll

_.bindAll(object, *methodNames)

methodNames指定的对象上绑定多个方法,只要它们被调用,就会在该对象的上下文中运行。非常方便的绑定,将要用作事件处理程序,否则就会有相当无用调用的函数methodNames是必需的。

var buttonView = { label : 'underscore', onClick: function(){ alert('clicked: ' + this.label }, onHover: function(){ console.log('hovering: ' + this.label } }; _.bindAll(buttonView, 'onClick', 'onHover' // When the button is clicked, this.label will have the correct value. jQuery('#underscore_button').on('click', buttonView.onClick

partial

_.partial(function, *arguments)

通过填充任意数量的参数来部分应用函数,而不改变其动态值。紧密的表亲。您可以在参数列表中传递_ 以指定不应预先填充的参数,而是在通话时保持开放状态。

var subtract = function(a, b) { return b - a; }; sub5 = _.partial(subtract, 5 sub5(20 => 15 // Using a placeholder subFrom20 = _.partial(subtract, _, 20 subFrom20(5 => 15

memoize

_.memoize(function, [hashFunction])

通过缓存计算结果来记忆给定的函数。用于加速运行缓慢的计算。如果传递了一个可选的hashFunction,它将被用于根据原始函数的参数计算用于存储结果的散列键。默认的hashFunction只使用memoized函数的第一个参数作为键。memoized值的缓存可用作返回函数的缓存属性。

var fibonacci = _.memoize(function(n) { return n < 2 ? n: fibonacci(n - 1) + fibonacci(n - 2 }

延迟

_.delay(function, wait, *arguments)

setTimeout 很像,等待毫秒后调用函数。如果您传递可选参数,则会在调用该函数时将它们转发给该函数

var log = _.bind(console.log, console _.delay(log, 1000, 'logged later' => 'logged later' // Appears after one second.

defer

_.defer(function, *arguments)

延迟调用函数,直到当前调用堆栈已被清除,类似于使用延迟为0的setTimeout。用于执行昂贵的计算或块中的HTML渲染,而不阻止UI线程更新。如果您传递可选参数,则会在调用该函数时将它们转发给该函数

_.defer(function(){ alert('deferred' } // Returns from the function before the alert runs.

throttle

_.throttle(function, wait, [options])

创建并返回传递函数的一个新的节制版本,当重复调用时,每调用一次等待几毫秒,它最多只会实际调用原始函数一次。对速度限制事件有用,速度限制事件的发生速度比您可以跟上的要快。

默认情况下,一旦您首次调用此功能,油门就会执行此功能,并且如果您在等待期内再次调用此功能,那么该时间段结束后即可执行此功能。如果您想禁用前沿呼叫,请传递{leading:false},并且如果您要禁用后沿执行,请传递

{trailing: false}.

var throttled = _.throttle(updatePosition, 100 $(window).scroll(throttled

debounce

_.debounce(function, wait, [immediate])

创建并返回传入函数的新的去抖动版本,该函数将延迟执行,直到自上次调用等待毫秒。用于实现只应在输入停止才会发生的行为。例如:渲染Markdown注释的预览,在窗口停止调整大小重新计算布局等等。

等待时间间隔结束时,将使用最近传递给去抖动函数的参数来调用该函数。

立即参数传递true 以使debounce触发前导函数而不是等待间隔的后沿。在防止意外双击“提交”按钮从而再次发射的情况下很有用。

var lazyLayout = _.debounce(calculateLayout, 300 $(window).resize(lazyLayout

once

_.once(function)

创建只能被调用一次的函数的一个版本。重复调用修改的函数将不起作用,从原始调用返回值。用于初始化函数,而不必设置布尔标志,然后再检查它。

var initialize = _.once(createApplication initialize( initialize( // Application is only created once.

after

_.after(count, function)

创建一个只在第一次被调用计数时间后运行的函数版本。在继续之前,用于对异步响应进行分组,这对于确保所有异步调用已完成的情况非常有用。

var renderNotes = _.after(notes.length, render _.each(notes, function(note) { note.asyncSave{success: renderNotes} } // renderNotes is run once, after all notes have saved.

before

_.before(count, function)

创建可以被调用的函数的版本不超过计数次数。最后一次函数调用的结果被记忆并在计数达到时返回。

var monthlyMeeting = _.before(3, askForRaise monthlyMeeting( monthlyMeeting( monthlyMeeting( // the result of any subsequent calls is the same as the second call

wrap

_.wrap(function, wrapper)

包裹第一函数的内包装功能,将它作为第一个参数。这允许包装器函数运行之前和之后执行代码,调整参数并有条件地执行它。

var hello = function(name) { return "hello: " + name; }; hello = _.wrap(hello, function(func) { return "before, " + func("moe") + ", after"; } hello( => 'before, hello: moe, after'

否定

_.negate(predicate)

返回谓词函数的新的否定版本。

var isFalsy = _.negate(Boolean _.find([-2, -1, 0, 1, 2], isFalsy => 0

撰写

_.compose(*functions)

返回函数列表的组合,其中每个函数都使用后面函数的返回值。在数学术语中,组成函数f()g()h()产生f(g(h()))

var greet = function(name){ return "hi: " + name; }; var exclaim = function(statement){ return statement.toUpperCase() + "!"; }; var welcome = _.compose(greet, exclaim welcome('moe' => 'hi: MOE!'