문제

So I've been reading a lot today about the with statement performance, readability and stuff like that but i'm still not sure whether i should or should not use the with statement in AS3.

Using the with statement seems the same as creating local var which would carry the reference. Can someone provide detailed information on this topic?

도움이 되었습니까?

해결책

Well, from my own personal benchmarks the with() statement actually showed up to be a tad bit slower. This isn't a big deal unless you are trying to squeeze every last ounce of performance out of your movie. The same could be said for doing for loops against variables instead of constants, or while loops vs for loops. Unless you are compounding these loops in lots of iterations you don't really need to be concerned with it.

As for code readability, I think it makes for a cleaner look using with(). It all comes down to coding style. The only time I really use the with() statement is with say an known target. Lets say you have your mouse event handler selecting objects on click, and setting it as currentObject. You then can have a function that does something like

with(currentObject)
{
 x = 100;
 y = 100;
 alpha = 0.5;
}

Sure you could do it other ways, but like I said. It comes down to coding style. The only savings I really see by using this method is shortened keystrokes vs doing currentObject.x = 100; currentObject.y = 100;

다른 팁

I think, biggest problem of with that it's not compile-time checked. Any errors will be discovered only in runtime. But it helps to clean code, and I somewhat like it. Just know what you doing, and it may be useful.

with is a pure syntactic sugar.

Performance-wise, it's clearly not an optimization. I've run a loop a few times and it seems to be even slower than usual attribute access.

In terms of readability, I'm not even sure there's a gain. I would actually prefer using Grant Skinner proposal. But there's a case where it might be useful: with (event.target as MovieClip), because it would require an intermediate variable declaration otherwise.

Conclusion: In most cases, don't use it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top