Once, I saw an example like this:
var a, x, y;
var r = 10;
with (Math) {
a = PI * r * r;
x = r * cos(PI);
y = r * sin(PI / 2);
}
And it looks very convenience, because that way I don’t have to type all the Math.s.
But when I take a look at the MDN, it says:
Using
withis not recommended, and is forbidden in ECMAScript 5 strict mode. The recommended alternative is to assign the object whose properties you want to access to a temporary variable.
So is it okay to use with()? In HTML5?
The MDN you linked says
Using with is not recommended…withis an excellent way of making spaghetti code for lunch.You might like it, but the guy that will need to debug it will curse you.
javascripthas some very weird operators, like the comma operator(,).Can you understand what the following code does?
Well it swaps a and b… You don’t understand
,so as the guy that will need maintain yourwithcode. Don’t use hacks, hacks are cool in charades games, not in real code.When is the comma operator useful?
The comma swap Fiddle