Files
freeCodeCamp/guide/portuguese/javascript/with/index.md
Randell Dawson 0a1eeea424 fix(guide) Replace invalid prism code block names (#35961)
* fix: replace sh with shell

fix replace terminal with shell

fix replace node with js

fix replace output with shell

fix replace cs with csharp

fix replace c++ with cpp

fix replace c# with csharp

fix replace javasctipt with js

fix replace syntax  with js

fix replace unix with shell

fix replace linux with shell

fix replace java 8 with java

fix replace swift4 with swift

fix replace react.js with jsx

fix replace javascriot with js

fix replace javacsript with js

fix replace c++ -  with cpp

fix: corrected various typos

fix: replace Algorithm with nothing

fix: replace xaml with xml

fix: replace solidity with nothing

fix: replace c++ with cpp

fix: replace txt with shell

fix: replace code with json and css

fix: replace console with shell
2019-05-15 19:08:19 +02:00

2.0 KiB

title, localeTitle
title localeTitle
With Com

Com

JavaScript with instrução é uma forma abreviada de editar várias propriedades em um objeto. A maioria dos desenvolvedores desencorajar o uso de with , e você é melhor não usar esta palavra-chave.

Nota : "strict mode" proíbe o uso de with .

Sintaxe

with (expression) 
  statement 

Exemplo de uso

Em JavaScript, você pode modificar individualmente as propriedades de um objeto, como abaixo:

let earth = {}; 
 earth.moons = 1; 
 earth.continents = 7; 

with lhe dá uma abreviação para modificar as propriedades em um objeto:

with (earth) { 
  moons = 1; 
  continents = 7; 
 } 

Enquanto este exemplo é inventado, você pode entender casos de uso with mais se você tiver objetos maiores como abaixo:

earth.continents.australia.geography.ocean = "Pacific"; 
 earth.continents.australia.geography.river = "Murray"; 
 earth.continents.australia.geography.mountain = "Kosciuszko"; 

Alternativas

Você não deve usar with , pois tem erros sutis e problemas de compatibilidade. Uma abordagem altamente recomendada é atribuir o objeto a uma variável e, em seguida, modificar as propriedades da variável. Aqui está um exemplo usando um objeto maior:

let earth = { 
  continents: { 
    australia: { 
      geography: {} 
    } 
  } 
 }; 
 
 let geo = earth.continents.australia.geography; 
 
 geo.ocean = "Pacific"; 
 geo.river = "Murray"; 
 geo.mountain = "Kosciuszko"; 

Experimente

https://repl.it/Mixg/5

Mais Informações:

https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Statements/with

https://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/

http://2ality.com/2011/06/with-statement.html