【问题标题】:what is one example of the usage of the jquery noop functionjquery noop 函数的用法之一是什么
【发布时间】:2014-03-11 04:11:29
【问题描述】:

扩展此链接What is the use of jQuery.noop() function?

You can use this empty function when you wish to pass 
around a function that will do nothing.

This is useful for plugin authors who offer optional callbacks; 
in the case that no callback is given, something like jQuery.noop could execute.

提供可选回调的插件作者是什么意思,一个例子就可以了。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    例如,带有可选完成失败并且总是回调的插件:

    options: {
        done: $.noop,
        fail: $.noop,
        always: $.noop
    }
    

    现在插件作者可以假设每个都定义了一个回调,即使使用插件的开发人员没有通过一个。

    https://github.com/jquery/jquery/blob/1.9-stable/src/core.js#L577

    【讨论】:

    • 尽管在大多数情况下,将其保留为未定义并对其进行测试更安全,但当您假设插件使用不正确时会导致错误(这可能是也可能不是坏事)
    • 所以你几乎可以说它是 function() 的简写{}
    • 就是这样。
    猜你喜欢
    • 2011-01-05
    • 2020-09-08
    • 2020-11-17
    • 2012-09-06
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2011-04-17
    • 2015-12-12
    相关资源
    最近更新 更多