javascript 如何正确返回空函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17026092/
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 06:49:42  来源:igfitidea点击:

How to properly return an empty function?

javascriptjslint

提问by Frédéric Hamidi

I'm using a run-time assignment of functions to account for browser differences. However for un-supported browsers, I want to return an empty function so that a JavaScript error is not thrown.

我正在使用功能的运行时分配来解决浏览器差异。但是,对于不受支持的浏览器,我想返回一个空函数,以免引发 JavaScript 错误。

But, jslint complains about empty functions. What is the jslint happy way to do this?

但是,jslint 抱怨空函数。什么是 jslint 快乐的方式来做到这一点?

Empty block.

空块。

$R.functionNull = function () {
    // events not supported;
};

$R.Constructor.prototype.createEvent = (function () {
    if (doc.createEvent) {
        return function (type) {
            var event = doc.createEvent("HTMLEvents");
            event.initEvent(type, true, false);
            $NS.eachKey(this, function (val) {
                val.dispatchEvent(event);
            });
        };
    }
    if (doc.createEventObject) {
        return function (type) {
            var event = doc.createEventObject();
            event.eventType = type;
            $NS.eachKey(this, function (val) {
                val.fireEvent('on' + type, event);
            });
        };
    }
    return $R.functionNull;
}());

回答by Frédéric Hamidi

You can add a body to your function and have it return undefined:

您可以向您的函数添加一个主体并让它返回undefined

$R.functionNull = function() {
    // Events not supported.
    return undefined;
};

This keeps the same semantics as a "truly empty" function, and should satisfy JSLint.

这与“真正的空”函数保持相同的语义,并且应该满足 JSLint。

回答by Nano Miratus

For me this works best:

对我来说,这最有效:

emptyFunction = Function();

console.log(emptyFunction); // logs '? anonymous() {}'
console.log(emptyFunction()); // logs 'undefined'

It's so short that I wouldn't even assign it to a variable (of course you can also use a constant-like variable "EF" or so, that's even shorter and doesn't need the additioal "()" brackets). Just use "Function()" anywhere you need a truly empty function, that doesn't even have a name, not even when you assign it to a variable, and that's the small behaviour difference between my solution and Frédéric's:

它太短了,我什至不会将它分配给一个变量(当然,你也可以使用类似常量的变量“EF”左右,它更短,不需要额外的“()”括号)。只需在需要真正空函数的任何地方使用“Function()”,该函数甚至没有名称,即使将其分配给变量时也不行,这就是我的解决方案与 Frédéric 的解决方案之间的微小行为差异:

// --- Frédéric ---

emptyFunction = function() {
   return undefined;
}

console.log(emptyFunction.name); // logs '"emptyFunction"'

// --- me ---

emptyFunction = Function();

console.log(emptyFunction.name); // logs '""' (or '"anonymous"' in chrome, to be fair)

回答by splintor

Use the lambda expression:

使用 lambda 表达式:

$R.functionNull = () => void 0;

回答by Michal ?ervenka

What about returning

回来怎么办

return () => undefined;

return () => undefined;

instead of

代替

return $R.functionNull;

return $R.functionNull;