1

通常,for..in臭名昭著的警告的解决方案是:

for(var prop in obj) {
    if(obj.hasOwnProperty(prop)) {
        foo(); bar(); baz();
    }
}

我觉得这样做会更干净:

for(var prop in obj) {
    if(!obj.hasOwnProperty(prop)) continue;
}

问题是……它们在功能上不一样吗?

4

2 回答 2

4

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"

于 2012-11-19T15:17:02.183 回答
1

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"}

于 2012-11-19T15:16:30.137 回答