views:

116

answers:

5

is this part of the native javascript lib? which browser support it?

+2  A: 

It's part of the JavaScript 1.5 specification. So it must be supported by major browser.

Philippe
The new ECMAScript 5 edition removes the `with` statement in strict mode. Future versions will probably remove it altogether.
Ionuț G. Stan
+1  A: 

Not a part of any native JS spec I've ever seen, and some quick Google-fu yields no result either. Not to say it isn't in there somewhere, but I'd guess that if it exists it's not well documented or supported.

Edit: beaten to the punch by Philippe, and apparently my answer is wrong. I'll leave it here for humility's sake, though. ;)

Scottie
Nelson says: "HA! HA!"
Arnis L.
+2  A: 

Yes it is part of it. Every browser that supports JavaScript 1.5 supports it (that is all major browsers, or grade A).

However, it is not recommended to use the with statement.

olle
+1. `with()` just isn't worth the hassle, especially since you can accomplish pretty much the same thing more explicitly using `var d = a.b.c; d.attribute = ...;`
Grant Wagner
A: 

Beware that JavaScript's with statement should be avoided.

See: with Statement Considered Harmful

Jesper
+1  A: 

Even though I do agree that it should not be used to just avoid declaring another variable (e.g.)

// BAD use of with is to replace this:
// some.expression.with.lots.of.dots.x = 10;
// some.expression.with.lots.of.dots.y = 20;
// with this:
with (some.expression.with.lots.of.dots) {
    x = 10;
    y = 20;
}

I do think that it has a reasonable use:

// Acceptable use of with is to close over a variable by value.
var functions = [];
for (var i = 0; i < 5; i++) {
    with ({ j: i }) {
        functions[i] = function() { return j; };
    }
}

The other option for the closure is a nested function, which has its advantages, but I find the debug experience being better if using with.

erikkallen