Javascript 中的空函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17685745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:12:21  来源:igfitidea点击:

Empty functions in Javascript

javascriptjslint

提问by

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?

然后稍后在代码中使用 blah,表示删除空块的 JSLint 提示是什么?

回答by The Alpha

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

我不知道是什么jsLint想法,但如果这是一个问题并且您需要一个解决方案,那么您可以执行以下操作:

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

Update :I think, Bergi's guess is right because, on the jslintsite in the Required Blockssection :

更新:我认为,我Bergi的猜测是正确的,因为在jslint站点上的Required Blocks部分:

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:

JSLint 期望 if、while、do 和 for 语句将使用块{即语句括在大括号中}。JavaScript 允许 if 写成这样:if(条件)语句;已知这种形式有助于许多程序员都在处理相同代码的项目中的错误。这就是 JSLint 期望使用块的原因:

if (condition) { statements; }

Experience shows that this form is more resilient.

经验表明,这种形式更具弹性。

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

因此,它可能只是检查空块{ }并使空白函数无效。

回答by Mat

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

如果你问什么 JsLint 选项关闭这个警告,它是:“debug:true”

Strangely, the docs make no reference to this behavior:

奇怪的是,文档没有提到这种行为:

"Tolerate debugger statements" | debug| trueif debuggerstatements should be allowed. Set this option to falsebefore going into production.

“容忍调试器语句” | 调试| 如果应该允许调试器语句,则为true。在投入生产之前将此选项设置为false

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

但是,如果您查看代码,您会发现调试选项设置为 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');
    }
    ...
}

回答by Michael

A lot of code checkers check for this sort of thing. It doesn't mean you should neverhave empty code blocks. Sometimes there are valid reasons for having them. But it oftenmeans 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 ... */ };

回答by splintor

Use the lambda expression:

使用 lambda 表达式:

const blah = () => void 0;

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

这将表明这blah是一个返回 的空函数undefined

回答by Aaren Cordova

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

如果您打算将该函数用作带有new运算符的构造函数:

// 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:

另一方面, if 是一个没有返回值的空白函数:

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

回答by jacobianism

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.

被视为代码块,提示让您知道它是空的(其中没有命令)。不过,正如@Katana314 所说,您不必将其删除,这可能是故意的。

回答by Basheer AL-MOMANI

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;

所以不要创建一个空函数,至少添加一个像 void(0); 这样的语句。或返回空;