简体   繁体   English

是否有 JavaScript 相当于 Python pass 语句什么都不做?

[英]Is there a JavaScript equivalent of the Python pass statement that does nothing?

I am looking for a JavaScript equivalent of the Python:我正在寻找与 Python 等效的 JavaScript:

pass statement that does not run the function of the ... notation? pass语句不运行...符号的 function?

Is there such a thing in JavaScript? JavaScript里面有这种东西吗?

Python's pass mainly exists because in Python whitespace matters within a block. Python 的pass主要存在是因为在 Python 中空格在块内很重要。 In Javascript, the equivalent would be putting nothing within the block, ie {} .在 Javascript 中,相当于在块中不放任何东西,即{}

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来自https://eslint.org/docs/rules/no-empty 的参考

python's pass is 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
}

Javascript does not have a python pass equivalent, unfortunately.不幸的是,Javascript 没有等效的 python pass

For example, it is not possible in javascript to do something like this:例如,在 javascript 中不可能做这样的事情:

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

Instead, we must do this:相反,我们必须这样做:

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语句。

I know this is a very old question but i guess that is also possible to do something like this.我知道这是一个非常古老的问题,但我想也可以做这样的事情。
You can declare a constant that contains a string (or something else).您可以声明一个包含字符串(或其他内容)的常量。

const pass = 'pass';

const pass = null; may also be good.也可能不错。

if (condition) {
   pass
} else {
   console.log('hi!');
}

However note also that this may be a better option.但是还要注意,这可能是更好的选择。

if (condition) {}
else {
    console.log('cool!');
}

Python doesn't have brackets to determine where the blocks of code are like javascript, so an empty block throws error (that's why you put the pass statement in empty blocks). Python 没有括号来确定代码块的位置,就像 javascript,因此空块会引发错误(这就是您将pass语句放在空块中的原因)。 What i have done by answering this question is just creating a constant using it as if it was a statement.我通过回答这个问题所做的只是创建一个常量,就好像它是一个语句一样。 The concept is really near to python's substitution of pass with ellipsis.这个概念非常接近 python 用省略号替换 pass 。
Someone in python prefers to use ... instead of pass . python 中的某个人更喜欢使用...而不是pass

if condition:
    ...
else:
    print('Python!')

In some cases pass can just be ;在某些情况下, pass可能只是;

A real life example can be:一个现实生活中的例子可以是:

var j;
for (j = i + 1; j < binstrN.length && binstrN[j] != 1; j++) {
}
let count = j - i;

is same as

var j;
for (j = i + 1; j < binstrN.length && binstrN[j] != 1; j++);
let count = j - i;

Here we are trying to move j to next '1', while i was already at a '1' before it, hence count gives the distance between first two '1's in the string binary string binstrN在这里,我们试图将j移动到下一个 '1',而i在它之前已经处于 '1',因此count给出字符串二进制字符串binstrN前两个 '1' 之间的距离

you could create a function that actually does nothing.你可以创建一个实际上什么都不做的函数。

const pass = () => {}
try {
  pass()
} else {
  console.log('helloworld!')
}

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

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

If you want to just use the pass operator in a ternary operator or just in an if statement in JS, you can do this:如果您只想在三元运算符中或仅在 JS 中的 if 语句中使用 pass 运算符,您可以这样做:

a === true && console.log('okay')

You can use also use ||您也可以使用 || operator but you should know that the ||运算符,但您应该知道 || is the opposite of &&.与&&相反。 Then if you want to use Pass in a function or a block in general as we do in Python like this:然后,如果您想像我们在 Python 中那样在函数或块中使用 Pass ,如下所示:

def Func(): pass

In JS you should just leave the block empty as this:在 JS 中,您应该将块留空,如下所示:

 if(){ 
    console.log('ok')
    }else{}

In the end, there are no braces in Python, so this is the main reason why we have a pass.最后,Python中没有大括号,所以这是我们有pass的主要原因。

An easy way for passing an if statement is entering a string.传递 if 语句的一种简单方法是输入字符串。 'pass' is more readible. “通过”更具可读性。

 if(true){ 'pass' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM