Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript with() function

Tags:

javascript

Is with() part of the native JavaScript library? Which browsers support it?

like image 205
cometta Avatar asked Aug 18 '09 13:08

cometta


People also ask

What is with () in JavaScript?

The 'with' statement adds the given object to the head of this scope chain during the evaluation of its statement body. If an unqualified name used in the body matches a property in the scope chain, then the name is bound to the property and the object containing the property.

What does function () do in JavaScript?

Points to Remember : JavaScript a function allows you to define a block of code, give it a name and then execute it as many times as you want. A function can be defined using function keyword and can be executed using () operator.

What is IFFI in JavaScript?

An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. The name IIFE is promoted by Ben Alman in his blog.

How do you call a function within a function in JavaScript?

To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.


5 Answers

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

like image 56
Philippe Avatar answered Nov 15 '22 06:11

Philippe


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.

like image 45
olle Avatar answered Nov 15 '22 06:11

olle


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.

like image 42
erikkallen Avatar answered Nov 15 '22 06:11

erikkallen


Beware that JavaScript's with statement should be avoided.

See: with Statement Considered Harmful

like image 22
Jesper Avatar answered Nov 15 '22 06:11

Jesper


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. ;)

like image 35
Scott Avatar answered Nov 15 '22 07:11

Scott