It is only to facilitate typing when you will access multiple members of an object.
As your example shows you can type only nome
and didn’t need to type obj.nome
to access the member.
The gain is very small and it can bring ambiguity problems so its use is not recommended. Consider it as something not existing in the language. Of course you can use in some case where it becomes 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, which nome
it will use, the parameter or the member of obj
? Note that it gets worse if you have a global flame variable 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 don’t have an IDE that helps) or the text gets too large you can solve this by making the name very short. And it’s called saving typing the o.
is important, there 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 the 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 only. Of course this is only worth doing if you will use the object several times.
As always, if you have a good reason in a specific situation and you are fully aware that there will be no problems there, you can even use it. In fact, there are those who demonstrate good utility of with
. This may be a legitimate form of use:
with({f:open("x.txt")}){
var data = f.read(1);
}
I put in the Github for future reference.
In Strict mode cannot use this syntax.
Documentation on the MDN.
Very good! Actionscript also has, now I see why many programmers do not use it!
– bio
@bigown, I see a utility in the case of a template engine. Because, instead of accessing the scope through the object passed by parameter, we can use
with
to access as if the properties were the variable. Classic example isunderscore.js
. has an excerpt similar to this:with(obj || {}) {}
– Wallace Maxters
The reply of the friend Marco Paulo Ollivier (below) is also very pertinent because it addresses scopos, which is something that sometimes confuses in javascript by the fact that simple blocks do not create scopo.
– Silvio Lucena Junior
The use that bigdown called legitimate is a variable statement at the block level. Perhaps he could make that clearer in the answer.
– Édipo Costa Rebouças
Just as a curiosity, "with" is very old, appeared in Wirth’s original Pascal language.
– zentrunix
Interestingly, I didn’t even know this existed in the Javascript language, I never had to use something like this in my applications, I believe I didn’t know "with" anymore for this reason, and from what I understand, it is easily replaceable by the normalized construction.
– Ivan Ferrer
About the one, is worth a read in this article
– Ivan Ferrer