Fixed awkward wording (#28320)
This commit is contained in:
committed by
The Coding Aviator
parent
b6e6d28f25
commit
ebe9e8f66f
@ -3,7 +3,7 @@ title: With
|
|||||||
---
|
---
|
||||||
## With
|
## With
|
||||||
|
|
||||||
JavaScript's `with` statement is a shorthand way for editing several properties on one object. Most developers discourage usage of `with`, and you are best not using this keyword.
|
JavaScript's `with` statement is a shorthand way to edit several properties on one object. Most developers discourage its usage, with best practice being not to use `with`. See Alternatives below for other approaches.
|
||||||
|
|
||||||
**Note**: `"strict mode"` in ECMAScript 5 forbids usage of `with`.
|
**Note**: `"strict mode"` in ECMAScript 5 forbids usage of `with`.
|
||||||
|
|
||||||
@ -16,13 +16,13 @@ with (expression)
|
|||||||
|
|
||||||
|
|
||||||
### Example Usage
|
### Example Usage
|
||||||
In JavaScript, you can individually modify an object's properties like below:
|
In JavaScript, you can individually modify an object's properties as below:
|
||||||
```javascript
|
```javascript
|
||||||
let earth = {};
|
let earth = {};
|
||||||
earth.moons = 1;
|
earth.moons = 1;
|
||||||
earth.continents = 7;
|
earth.continents = 7;
|
||||||
```
|
```
|
||||||
`with` gives you a shorthand to modify the properties on an object:
|
`with` gives you a shorthand way to modify the properties on an object:
|
||||||
```javascript
|
```javascript
|
||||||
with (earth) {
|
with (earth) {
|
||||||
moons = 1;
|
moons = 1;
|
||||||
@ -30,7 +30,7 @@ with (earth) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
While this example is contrived, you can understand use cases of `with` more if you have larger objects like below:
|
While this example is contrived, you can understand cases of `with` better if you use larger objects as below:
|
||||||
```javascript
|
```javascript
|
||||||
earth.continents.australia.geography.ocean = "Pacific";
|
earth.continents.australia.geography.ocean = "Pacific";
|
||||||
earth.continents.australia.geography.river = "Murray";
|
earth.continents.australia.geography.river = "Murray";
|
||||||
@ -38,7 +38,7 @@ earth.continents.australia.geography.mountain = "Kosciuszko";
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Alternatives
|
### Alternatives
|
||||||
You should not use `with` as it has subtle bugs and compatibility issues. A highly recommended approach is to assign the object to a variable, and then modify the variable's properties. Here is an example using a larger object:
|
You should not use `with`, as it has subtle bugs and compatibility issues. A highly recommended approach is to assign the object to a variable, and then to modify the variable's properties. Here is an example using a larger object:
|
||||||
```javascript
|
```javascript
|
||||||
let earth = {
|
let earth = {
|
||||||
continents: {
|
continents: {
|
||||||
|
Reference in New Issue
Block a user