What real purpose does $.noop() serve in jQuery 1.4?

前端 未结 9 633
故里飘歌
故里飘歌 2020-12-30 19:01

Pouring over the release notes regarding jQuery 1.4, I came acrosss $.noop() which is:

Description: An empty function. (added in 1.4)

9条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 19:17

    If you have a function that accepts a function as a parameter, and you don't have any code to give it, you can pass $.noop.

    I can't think of any such cases in jQuery where the parameter isn't optional in the first place, though.

    Unlike writing function(){}, passing $.noop will not create a new function instance, saving a bit of memory. However, if whatever you're passing it to modifies the function object (eg, funcParam.id = 2), passing $.noop will mess things up.

提交回复
热议问题