是否有与 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
Is there a javascript equivalent for the python pass statement that does nothing?
提问by user781486
I am looking for a javascript equivalent for the python pass
statement that does nothing. Is there such a thing in javascript?
我正在寻找与 pythonpass
语句等效的 javascript ,它什么都不做。javascript中有这样的东西吗?
采纳答案by jakevdp
Python's pass
mainly 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
回答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 //pass
like 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
回答by danb4r
Javascript does not have a python pass
equivalent, 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 pass
statement, 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 if
statement.
使用该pass
语句的好处之一是,在开发过程中,我们可以从上面的三元运算符示例演变而来,而不必将其转换为完整的if
语句。