Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty functions in Javascript

People also ask

How do you declare an empty function?

An empty function is basically creating a function without defining any operations inside it. A class named Demo contains an empty function named 'my_empty_fun' which is just completed by placing two flower brackets, without adding any functionality into it.

What is an empty function called?

A "noop" or "null function": var noop = function(){}

Is empty condition in JavaScript?

Use the length property to check if a string is empty, e.g. if (str. length === 0) {} . If the string's length is equal to 0 , then it's empty, otherwise it isn't empty.

How do you do nothing in JavaScript?

Use Void(0) to Define Do Nothing to Keep User on the Same Page in JavaScript. First, we'll use the void operator in a situation when you need an undefined result. However, void(0) tells JavaScript to DO NOTHING and RETURN NOTHING.


I don't know what jsLint thinks but if this is a problem and you need a solution then you can do something like the following:

var blah = function() { return undefined; }; // or just return;

Update : I think, Bergi's guess is right because, on the jslint site in the Required Blocks section :

JSLint expects that if, while, do and for statements will be made with blocks {that is, with statements enclosed in braces}.JavaScript allows an if to be written like this:if (condition) statement;That form is known to contribute to mistakes in projects where many programmers are working on the same code. That is why JSLint expects the use of a block:

if (condition) { statements; }

Experience shows that this form is more resilient.

So, It probably just checks for empty blocks { } and invalidate the blank function.


Use the lambda expression:

const blah = () => void 0;

This will make it clear that blah is an empty function that returns undefined.


If you are asking what JsLint option turns this warning off it is: "debug:true"

Strangely, the docs make no reference to this behavior:

"Tolerate debugger statements" | debug | true if debugger statements should be allowed. Set this option to false before going into production.

But if you look at the code, you can see that it won't warn you with the debug option set to true:

function block(kind) {
    // A block is a sequence of statements wrapped in braces.

    ...

    if (kind !== 'catch' && array.length === 0 && !option.debug) {
        curly.warn('empty_block');
    }
    ...
}

A lot of code checkers check for this sort of thing. It doesn't mean you should never have empty code blocks. Sometimes there are valid reasons for having them. But it often means that the programmer just forgot to write the implementation. :)

What I like to do is put a comment in the function body, explaining why it's empty. This should suppress the warning, but it may not depending on whether the code checker considers a code block with a comment "empty".

var blah = function() { /* empty because ... */ };