What is the JavaScript convention for no operation?

前端 未结 6 1822
执念已碎
执念已碎 2020-11-30 18:00

What is the JavaScript convention for no operation? Like a Python pass command.

  • One option is simply an empty function: function() {}
6条回答
  •  日久生厌
    2020-11-30 18:59

    I use:

     (0); // nop
    

    To test execution time of this run as:

    console.time("mark");
    (0); // nop
    console.timeEnd("mark");
    

    result: mark: 0.000ms

    Using Boolean( 10 > 9) can be reduced it to simply ( 10 > 9) which returns true. Coming up with the idea to use a single operand I fully expected (0); would return false, but it simply returns the argument back as can be reviewed by performing this test at the console.

    > var a = (0);
    < undefined
    > a
    < 0
    

提交回复
热议问题