通常,for..in
臭名昭著的警告的解决方案是:
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
foo(); bar(); baz();
}
}
我觉得这样做会更干净:
for(var prop in obj) {
if(!obj.hasOwnProperty(prop)) continue;
}
问题是……它们在功能上不一样吗?
通常,for..in
臭名昭著的警告的解决方案是:
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
foo(); bar(); baz();
}
}
我觉得这样做会更干净:
for(var prop in obj) {
if(!obj.hasOwnProperty(prop)) continue;
}
问题是……它们在功能上不一样吗?
They are functionally identical. Period.
As for the matter of style, Douglas and his JSLint say: don't use continue
:
Avoid use of the continue statement. It tends to obscure the control flow of the function.
See http://javascript.crockford.com/code.html and search for "continue"
Yes, they are functionally identical.
I'd say the continue-less example is a little more semantic, though.
In the first example, you aren't negating the bool in your if, so, you have a typical
if(property){"Execute the rest of this loop"}
instead of
if(not property){"Do not execute the rest of this loop"
}