What is the "with" in JavaScript?

58

What is with in JavaScript?

Does it only work to get object values as if it were a variable, or is it also possible to set or change properties through it?

Example:

var obj = {nome: 'Stack Overflow'}

with (obj) {
  console.log(nome); //Imprime: 'Stack Overflow'
}
    
asked by anonymous 02.03.2015 / 13:59

3 answers

49

It's just for easy typing when you're going to access multiple members of an object.

As your example shows you can only type nome and you did not have to type obj.nome to access the member.

The gain is very small and it can cause ambiguity problems so its use is not recommended. Consider it as something non-existent in language. Of course you can use in some case where it is clear there is no ambiguity and you will access many members of an object of a very long name, but still the gain is very small.

Ambiguity can occur because you no longer know if you are accessing a local variable (including parameter), global, an existing property in the prototype, or if you are referring to a member of the object. Example using your object:

function f(nome, obj) {
    with (obj) {
        console.log(nome);
    }
}

And now, what nome will it use, the parameter or member of obj ? Note that it gets worse if you have a global variable called nome - although it should not have global variables.

If you have a very large object name and think that in addition to typing a lot (if you do not have an IDE that helps) or the text gets too large you can solve this by making the name very short. And it is called that saving the typing of o. is important, it should rethink its coding criteria:

var o = objeto_de_nome_muito_grande_mas_que_nao_deveria_ter_sido_nomeado_assim;
console.log(o.nome);

Using with :

with({o:objeto_de_nome_muito_grande_mas_que_nao_deveria_ter_sido_nomeado_assim}){
    console.log(o.nome);
};

The examples are for demonstration purposes only. Obviously it's only worth doing this if you use the object several times.

As always, if you have good reason in a particular situation and are fully aware that there will be no problems there, you can even use them. In fact, some demonstrate with good . This can be a legitimate way to use:

with({f:open("x.txt")}){
    var data = f.read(1);
}

In strict mode is not possible use this syntax.

MDN Documentation .

    
02.03.2015 / 14:13
13

In JavaScript, with is a language resource capable of circumventing the lexical scope, that is, regardless of the array order of the variables, with will take an expression passed as a reference as its scope. It is the native way of extending the scope of an instruction, acting as a shortcut for recurring access to an expression.

function foo(x, respostas) {
with (respostas) {
       x = 2; // Estamos atribuindo um novo valor à variável x dentro de o
}

if( (x + x) === respostas.x) { // 1 + 1 = 2 ?
       console.log('Sabemos somar');
}

console.log('Confira a resposta: ' + (x + x)); // Ooops ... retorna 4; isso pode não ser esperado
}

var o = {
       y : 2
};

f(1,o);

If the object passed by reference to with does not have one of the manipulated attributes, the compiler will raise its declaration to the nearest scope in the hierarchy, in our case the scope of foo . No warning will be posted and the variable will be changed in the closest scope. If it has an identifier with the same denomination, the Compiler will perform an assignment (LHS: lefthand-side) and our value in the context of foo will be lost.

Source: link

    
02.03.2015 / 15:16
2

JavaScript is controversial. A few years ago, the mozilla showed as deprecated in the documentation, around 2010. Later was removed from this condition placed only a warning to avoid the use. It is still unclear whether it will really be removed or not.
Currently the notice in the documentation is quite explicit about the importance in NOT to use. Therefore it is important to follow the recommendation.

I've never used it in the same way as in the examples of other answers. I have always used it only to assist in accessing properties of an object. As an example, "simulate" namespace as in PHP.

Obviously it goes far from the namespace functionality in PHP.

Example:

<script>

Foo = {
    Sample1: function (param1, param2) {
        return param1 + ' statement is ' + param2 + '.';
    },
    Sample2: function () {
        return 'don\'t cry, baby';
    }
};

with(Foo) {
    document.write(Sample1('with', 'deprecated') + ' ' + Sample2());
}
</script>

The code gets cleaner clean.

If you had functions with the same names, under different contexts, you just had to create a namespace for both and it was resolved in an "elegant" way.

It's a cool resource that helps organize code avoiding conflicts between scripts from different libraries. * When they are organized within the scope of an object .

It still works and will work for a few more years. Maybe until 2025 or a little later (I invented the year here at the time, blz?). But we must follow the recommendation of the documentation. link

I just posted this because I have not seen in any of the other highly positive responses to talk about NOT recommending statement with .

Note: The above example is didactic for illustration purposes. For the focus here is not on how to simulate namespace.

    
25.10.2016 / 19:16