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

前端 未结 9 658
感情败类
感情败类 2021-02-06 20:02

I am looking for a JavaScript equivalent of the Python:

pass statement that does not run the function of the ... notation?

Is the

相关标签:
9条回答
  • 2021-02-06 20:26

    python's pass is required for empty blocks.

    try:
        # something
    except Exception:
        pass
    

    In javascript you can simply catch an empty block

    try {
        // some code
    } catch (e) {
        // This here can be empty
    }
    
    0 讨论(0)
  • 2021-02-06 20:29

    Javascript does not have a python pass equivalent, unfortunately.

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

    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.

    0 讨论(0)
  • 2021-02-06 20:35

    you could create a function that actually does nothing.

    const pass = t => t
    
    try {
      pass()
    } else {
      console.log('helloworld!')
    }
    
    0 讨论(0)
  • 2021-02-06 20:38

    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. {}.

    0 讨论(0)
  • 2021-02-06 20:41

    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) { ; }
    
    0 讨论(0)
  • 2021-02-06 20:42

    I know this is a very old answer but i guess that is also possible to do something like this.
    You can declare a constant that contains a string.

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

    However note also that this may be a better option.

    if (condition) {}
    else {
        console.log('cool!')
    }
    
    0 讨论(0)
提交回复
热议问题