Empty functions in Javascript

血红的双手。 提交于 2019-12-04 15:15:13

问题


If I have something like this:

var blah = function() { };

and then later in code blah is being used, what is the JSLint hint that says remove the empty block?


回答1:


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.




回答2:


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');
    }
    ...
}



回答3:


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 ... */ };



回答4:


Use the lambda expression:

const blah = () => void 0;

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




回答5:


If you intend to use the function as a constructor with the new operator:

// Returns the instance that was just created with the new operator.
var ClassLikeFunction = function(){
    return this; 
};

On the other hand, if is intentionally a blank function with no return value:

// Returns the same value as a function that returned nothing.
var blankFunction = function(){
    return undefined; 
};



回答6:


This

{
    ...
}

is considered a code block and the hint is letting you know that it is empty (there are no commands in it). You don't have to remove it though, as @Katana314 said, it could be intentional.




回答7:


what let me to this question is that I had an empty function in my namespace

and when I called that function, and

TypeError: MyNamespcae.myFunction is not a function

so don't create an empty function, at lease add one statement like void(0); or return null;



来源:https://stackoverflow.com/questions/17685745/empty-functions-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!