Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the noop function in jQuery?

Tags:

jquery

I found in an article on function .noop(), which in this library should be included. What is doing? Just call fallbacks? How to use this function?

This method does not accept any arguments.

http://api.jquery.com/jQuery.noop/

like image 460
Mardzis Avatar asked Sep 17 '25 09:09

Mardzis


2 Answers

Well, it does state in the docs:

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

Probably a fast way to create a default function. And as an added bonus, without creating an anonymous function every time that function is called.

function aFunctionThatAcceptsACallback(callback){
  callback = callback || jQuery.noop;

  // do something

  // safely call callback, regardless if the function received any
  callback();
}
like image 200
Joseph Avatar answered Sep 18 '25 21:09

Joseph


$.noop() is an empty function which returns nothing.

In another word, it's just equivalent to this:

$.noop => function () {}//returns nothing

It has nothing to do with the function so it no longer requires to accept arguments.

like image 44
Bhojendra Rauniyar Avatar answered Sep 18 '25 23:09

Bhojendra Rauniyar