JavaScript with function ()

Is with()part of the built-in JavaScript library? What browsers support it?

+3
source share
5 answers

This is part of the JavaScript 1.5 specification . Therefore, it must be supported by the main browser.

+4
source

Yes, that is part of it. Each browser that supports JavaScript 1.5 supports it (these are all major browsers or class A).

However, using the with statement is not recommended .

+2
source

, , , (.)

// 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;
}

, :

// 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; };
    }
}

, , , , .

+2

- JS, - , Google-fu . , - , , , .

: , , , . .;)

+1

, JavaScript with.

: ,

+1

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


All Articles