mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-17 22:01:24 -04:00
* Updated some redaction errors I just fixed some redaction errors in spanish language. * fix: removed syntax for triple backticks
77 lines
2.0 KiB
Markdown
77 lines
2.0 KiB
Markdown
---
|
|
title: With
|
|
localeTitle: With
|
|
---
|
|
## With
|
|
|
|
La declaración de JavaScript `with` es una forma abreviada de editar varias propiedades en un objeto. La mayoría de los desarrolladores desalentan el uso de `with`, por eso es mejor no usar esta palabra clave.
|
|
|
|
**Nota** : `"strict mode"` prohíbe el uso de `with` .
|
|
|
|
### Sintaxis
|
|
|
|
```
|
|
with (expression)
|
|
statement
|
|
```
|
|
|
|
### Ejemplo de uso
|
|
|
|
En JavaScript, puedes modificar individualmente las propiedades de un objeto como se muestra a continuación:
|
|
|
|
```javascript
|
|
let earth = {};
|
|
earth.moons = 1;
|
|
earth.continents = 7;
|
|
```
|
|
|
|
`with` le da una forma de modificar las propiedades de un objeto:
|
|
|
|
```javascript
|
|
with (earth) {
|
|
moons = 1;
|
|
continents = 7;
|
|
}
|
|
```
|
|
|
|
Si bien este ejemplo está prediseñado, se pueden comprender los casos de uso de `with`, más si tiene objetos más grandes como el siguiente:
|
|
|
|
```javascript
|
|
earth.continents.australia.geography.ocean = "Pacific";
|
|
earth.continents.australia.geography.river = "Murray";
|
|
earth.continents.australia.geography.mountain = "Kosciuszko";
|
|
```
|
|
No debe utilizar `with` ya que tiene errores sutiles y problemas de compatibilidad.
|
|
|
|
### Alternativas
|
|
|
|
Un enfoque altamente recomendado es asignar el objeto a una variable y luego modificar las propiedades de la variable. Aquí hay un ejemplo usando un objeto más grande:
|
|
|
|
```javascript
|
|
let earth = {
|
|
continents: {
|
|
australia: {
|
|
geography: {}
|
|
}
|
|
}
|
|
};
|
|
|
|
let geo = earth.continents.australia.geography;
|
|
|
|
geo.ocean = "Pacific";
|
|
geo.river = "Murray";
|
|
geo.mountain = "Kosciuszko";
|
|
```
|
|
|
|
### Pruébalo
|
|
|
|
https://repl.it/Mixg/5
|
|
|
|
#### Más información:
|
|
|
|
[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with)
|
|
|
|
[https://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/](https://yuiblog.com/blog/2006/04/11/with-statement-considered-harmful/)
|
|
|
|
[http://2ality.com/2011/06/with-statement.html](http://2ality.com/2011/06/with-statement.html)
|