是否有与 python pass 语句等效的 javascript 什么都不做?

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

Is there a javascript equivalent for the python pass statement that does nothing?

javascriptpython

提问by user781486

I am looking for a javascript equivalent for the python passstatement that does nothing. Is there such a thing in javascript?

我正在寻找与 pythonpass语句等效的 javascript ,它什么都不做。javascript中有这样的东西吗?

采纳答案by jakevdp

Python's passmainly exists because in Python whitespace matters within a block. In Javascript, the equivalent would be putting nothing within the block, i.e. {}.

Python 的pass存在主要是因为在 Python 中空格在块内很重要。在 Javascript 中,相当于将任何内容放入块中,即{}.

回答by Amyth

python's passis required for empty blocks.

空块需要python的pass

try:
    # something
except Exception:
    pass

In javascript you can simply catch an empty block

在javascript中,您可以简单地捕获一个空块

try {
    // some code
} catch (e) {
    // This here can be empty
}

回答by user325441

I've found that I get an error with empty braces, instead I put a semicolon in there, basically the same thing:

我发现我在使用空大括号时出现错误,而是在那里放了一个分号,基本上是一样的:

try { //something; } catch (err) { ; }

回答by Sirius

use //passlike python's pass

//pass像python一样使用pass

like:

喜欢:

if(condition){
   //pass
}

This is equivalent to leaving the block with nothing in it, but is good for readability reasons.

这相当于让块中什么都没有,但出于可读性原因是好的。

reference from https://eslint.org/docs/rules/no-empty

来自https://eslint.org/docs/rules/no-empty 的参考

回答by danb4r

Javascript does not have a python passequivalent, unfortunately.

pass不幸的是,Javascript 没有 Python等价物。

For example, it is not possible in javascript to do something like this:

例如,在 javascript 中不可能做这样的事情:

process.env.DEV ? console.log('Connected..') : pass

process.env.DEV ? console.log('Connected..') : pass

Instead, we must do this:

相反,我们必须这样做:

if (process.env.DEV) console.log('Connected..')

if (process.env.DEV) console.log('Connected..')

The advantage of using the passstatement, among others, is that in the course of the development process we can evolve from the above ternary operator example in this case without having to turn it into a full ifstatement.

使用该pass语句的好处之一是,在开发过程中,我们可以从上面的三元运算符示例演变而来,而不必将其转换为完整的if语句。