Use or not use with with statement in AS3?

So today I read a lot about performance, readability, etc., but I'm still not sure if I should or should not use the with statement in AS3.

Using a with statement is like creating a local var that will contain a link. Can someone provide detailed information on this topic?

+3
source share
3 answers

Well, in my own personal benchmarks, the with () operator actually showed that the bit is a bit slower. It doesn't really matter if you're not trying to squeeze every last ounce of performance out of your movie. The same could be said for loops against variables instead of constants, or while loops against loops. If you don't mix these loops in a lot of iterations, you really don't need to worry about that.

Regarding code readability, I think this makes a cleaner look using with (). It all comes down to the coding style. The only time I really use the with () operator is a known target. Suppose you have a mouse event handler that selects objects by click and sets it as currentObject. Then you can have a function that does something like

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

, -, , . . , , - currentObject.x = 100; currentObject.y = 100;

+2

, with , . . , . , , .

+2

with .

, . , , .

In terms of readability, I'm not even sure that there is a gain. I would rather use the Grant Skinner Offer . But there is a case where this may be useful: with (event.target as MovieClip)because otherwise an intermediate variable would be required.

Conclusion: in most cases, do not use it.

+1
source

Source: https://habr.com/ru/post/1773862/


All Articles