Skip to content

Commit

Permalink
Rather than declaring empty anonymous functions all around, introduce…
Browse files Browse the repository at this point in the history
… and use a single empty function. Thanks to Matt Kruse for the suggestion.
  • Loading branch information
jeresig committed Dec 31, 2009
1 parent fe6c86d commit 6cb2945
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ajax.js
Expand Up @@ -384,7 +384,7 @@ jQuery.extend({
// The request was aborted, clear the interval and decrement jQuery.active
if ( !xhr || xhr.readyState === 0 ) {
requestDone = true;
xhr.onreadystatechange = function(){};
xhr.onreadystatechange = jQuery.noop;

// Handle the global AJAX counter
if ( s.global && ! --jQuery.active ) {
Expand All @@ -394,7 +394,7 @@ jQuery.extend({
// The transfer is complete and the data is available, or the request timed out
} else if ( !requestDone && xhr && (xhr.readyState === 4 || isTimeout === "timeout") ) {
requestDone = true;
xhr.onreadystatechange = function(){};
xhr.onreadystatechange = jQuery.noop;

status = isTimeout === "timeout" ?
"timeout" :
Expand Down
2 changes: 2 additions & 0 deletions src/core.js
Expand Up @@ -464,6 +464,8 @@ jQuery.extend({
return true;
},

noop: function() {},

// Evalulates a script in a global context
globalEval: function( data ) {
if ( data && rnotwhite.test(data) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/event.js
Expand Up @@ -427,7 +427,7 @@ jQuery.event = {
ready: {
// Make sure the ready event is setup
setup: jQuery.bindReady,
teardown: function() {}
teardown: jQuery.noop
},

live: {
Expand Down
2 changes: 1 addition & 1 deletion src/offset.js
Expand Up @@ -121,7 +121,7 @@ jQuery.offset = {

body.removeChild( container );
body = container = innerDiv = checkDiv = table = td = null;
jQuery.offset.initialize = function() {};
jQuery.offset.initialize = jQuery.noop;
},

bodyOffset: function( body ) {
Expand Down

6 comments on commit 6cb2945

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on 6cb2945 Jan 4, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"noop" ? Isn't that: "no operation", abbreviated ? ... what was wrong with some combination of words "empty" and "function" ?
I think even: "emptyFunction" would work better.
After all in jQ we have "isArray", "isFunction" ...and other nice descriptive names ;o)

--DBJ

@0xqd
Copy link

@0xqd 0xqd commented on 6cb2945 Aug 12, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DBJDBJ: It's no operation perform which is quite meaningful I think.

@mubasshir
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DBJDBJ @rhacker hi, in the end of 2014, noop has become a thing. Thanks for addition of awesome function

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on 6cb2945 Nov 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mubasshir thanks .. although for clarity sake I do maintain my position:

"noop" is not an empty function

function is not an operator, thus empty function can not be any kind of operator ...
ps: sometimes emptyFunction is also called nullFunction , which is also OK.

          var emptyFunction = nullFunction = function () { } ;

@StarveTheEgo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very impressive.
No operation by the way.

@DBJDBJ
Copy link

@DBJDBJ DBJDBJ commented on 6cb2945 Jul 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we what, seven years after the last comment? I can proceed :) What is "very impressive"?

JavaScript has no NOP (aka noop) statement. Alternatives 2021, are:

  1. Function.prototype() -- accepts any arguments and returns undefined
  2. ES6 arrow function syntax -- const noop = () => {};

I vote 1. Name it anything you like. Whoever you are.

Please sign in to comment.