admin管理员组文章数量:1401384
As far as I know, JavaScript's EmptyStatement statement has two use-cases. It is used:
as an no-operation (no-op) mand in places where a statement is required, and
as a mechanism to ensure that the code is interpreted as intended in scenarios where the programmer omits semicolons from the end of statements (in which case the programmer inserts a semicolon (empty statement) at the beginning of lines that represent new statements but start with a
(
,[
,/
,+
, or-
token, in order to prevent that line from being interpreted as a continuation of the previous line (statement).)
I would love to see some real-world applications of the first use-case - using empty statements as no-op mands.
I can't remember ever using a semicolon as a no-op mand, but if you did, or remember someone else's code that does, could you show how it was used?
As far as I know, JavaScript's EmptyStatement statement has two use-cases. It is used:
as an no-operation (no-op) mand in places where a statement is required, and
as a mechanism to ensure that the code is interpreted as intended in scenarios where the programmer omits semicolons from the end of statements (in which case the programmer inserts a semicolon (empty statement) at the beginning of lines that represent new statements but start with a
(
,[
,/
,+
, or-
token, in order to prevent that line from being interpreted as a continuation of the previous line (statement).)
I would love to see some real-world applications of the first use-case - using empty statements as no-op mands.
I can't remember ever using a semicolon as a no-op mand, but if you did, or remember someone else's code that does, could you show how it was used?
Share Improve this question asked Aug 6, 2011 at 17:46 Šime VidasŠime Vidas 186k65 gold badges289 silver badges391 bronze badges 2- I can't think of a situation in JavaScript in which a statement is required. – Pointy Commented Aug 6, 2011 at 17:52
-
@Pointy Inside pound statements (for, if, while, ...). For instance:
if ( x ) ...
(after the header of an if-statement, a statement is required) – Šime Vidas Commented Aug 6, 2011 at 17:57
6 Answers
Reset to default 4Traverse to the next element node, placing all the logic in the expression evaluated for the while
.
// ---------empty statement-------------------------------------v
while( node && (node = node.nextSibling) && node.nodeType !== 1 );
// do something with the resulting node
Because all the logic (as well as the assignment) is in the expression being evaluated, there's no need for the Statement, so an empty one is used.
EDIT: Off topic a little.
These type of while
statements have sometimes bothered me when reading through a bunch of code quickly.
Perhaps a better form is:
do;while( node && (node = node.nextSibling) && node.nodeType !== 1 )
...so you can see right at the start that it is using an EmptyStatement, though it technically will always require 1 additional processing of the statement.
Interestingly, this jsPerf consistently gives an ever so slight edge to do-while
(in Chrome 13 and Firefox 5 anyway). For practical purposes, they are identical.
I sometimes write code like this:
if( somecondition )
doSomething();
else if( anothercondition )
doSomethingElse();
else if( thirdcondition )
; // we don't need to do anything in this case
else if( fourthcondition )
goFourthAndMultiply();
else
panic();
Of course, whether one uses ;
or {}
in these cases is just a stylistic choice.
One example I can think of, if for some reason you want to convert the following:
if(a) {
if(b) {
B()
} else {
C()
}
}
to use the ternary operator:
a ? (b ? B() : C()) : emptystatement;
This would be an example of the first case you state.
If you create a hyperlink which should not navigate you can do:
<a href="javascript:;" onclick="...">Click here</a>
This is a bit old-fashioned but it is (or rather was) a use case of it.
try {
some code
} catch(Exception) {;}
;
for(var i = 0, len = array.length; i < len; SomFunction(i++));
;
if (condition) return; else ;
Maybe it's useful for you ( a C example) :
for ( ;; ) {
}
or this one:
for(var i = 0, len = array.length; i < len; SomFunction(i++));
本文标签: Realworld applications of empty statements as noop commands in JavaScriptStack Overflow
版权声明:本文标题:Real-world applications of empty statements as no-op commands in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744309402a2599957.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论