Revive restricted with() statement

The with statement is not available at all in strict mode, because it leads to ambiguity and has other problems. However, would it be possible and sensible to reintroduce a subset of its functionality, e.g. limiting the parenthetical expression to predefined objects like Math?

var a, x, y;
var r = 10;

with (Math) {
  a = PI * r * r;
  x = r * cos(PI);
  y = r * sin(PI / 2);
} 

(Example courtesy of MDN.)

var a, x, y;
var r = 10;
const π = Math.PI;

a = π * r * r;
x = r * Math.cos(π);
y = r * Math.sin(π / 2);
var a, x, y;
var r = 10;

a = Math.PI * r * r;
x = r * Math.cos(Math.PI);
y = r * Math.sin(Math.PI / 2);

All predefined objects are mutable; you can add your own properties to Math, for example (not that it’s a good idea) - so that wouldn’t be any different than allowing it for normal objects.

1 Like

I think this is unlikely to happen, with is hated by a bunch of the committee. Additionally, I don't find with (Math) { … } to be any better than const { … } = Math for getting easy (and understandable) references to properties.

1 Like