开发者

Is the with keyword that bad in javascript? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Are there legitimate uses for JavaScript's “with” statement?

in "JavaScript the Good Parts", with is deemed to be a bad part of javascript, but look at this snippet:

var foo={foof:function(){console.log(this)}}
var fuu开发者_JAVA技巧={fuuf:function(){console.log(this)}}
with(foo){
     console.log(this);
 with(fuu){
     console.log(this);
     foof();
     fuuf();
 }
}

Is with really that bad a practice? Can with provide fun or an advantage sometimes, who can give a example?


That's convenient, but it still has a fundamental problem that can get you into trouble quickly. Consider this:

var foo={foof:function(){console.log(this)}}
var fuu={fuuf:function(){console.log(this)}}
with(foo){
     console.log(this);
 with(fuu){
     console.log(this);
     foof();
     fuuf();
 }
 myProp = "blah";
}

Now what happened when we assigned to myProp?

Hard to tell.

If foo contains a property named myProp, then its value is now "blah". But if it didn't, then we just assigned "blah" to the myProp of the global object (probably window). It's easy to tell what's going on in this trivial example, but what if foo had been defined 1000 lines earlier? Or in a different file? Not so easy then.

More info and an actual answer to your question here:

Are there legitimate uses for JavaScript's "with" statement?

And here:

http://www.yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/


The trouble with with is that it is somewhat ambiguous what you are doing. For instance, your foof() function call might as well be a function defined elsewhere. It is difficult to see at a glance what is being used.


I can give an example, john resigs simple (and wildly popular) client side templating function.

<editorial>
the good parts is a phenomenal book that changes lives, but crockford is not infallable, and often will declare something that is really a matter of taste (like one line if statements) is bad with the same authority that he says genuinely horrible things are bad (like the whole implicit global/explicit local thing).
<editorial>

The issue with with is addressed on the mdc page

 function f(x, o) {
  with (o)
    print(x);
 }

Basically, the value of x is ambiguous until run time. If o has a property called x, x will be that property. If not, x will be the thing passed in. That means developers reading the code may have a hard time figuring out what x means, and more importantly, the JS interpreter will not be able to apply certain kinds of optimizations it otherwise would be able to.

<opinion>
Now personally, I find with perfectly readable, and I would never ever name variables the way they are named in the above example, so I find the readability and ambiguity arguments hogwash. As for the perf argument, as long as we aren't talking about being silly (like having half your app inside a with block), is mostly an argument for premature optimization, where you are really talking about almost immeasurable amounts of wasted time in real world scenarios. I think it is a highly situational language feature that should only really ever be used when the alternative is either significantly more, or less readable code would be the alternative.
</opinion>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜