Ask EchoJS: Why isn't the `with` statement used more often?
▼4 up and 0 down, posted by
4 up and 0 down, posted by
The `with` statement [1] allows code execution in the scope of an object that we pass to it.
```
with (window.performance.timing) {
console.log(loadEventEnd - navigationStart);
}
```
Doesn't this make things simpler? Other than the reasons stated on MDN, are there any more serious problems?
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with
(pros/cons) (-) requires training - this syntax may be unfamiliar/confusing to some people (-) over-use is very bad - the problems are mentioned in MDN. (+) when used exactly right - it saves a few characters IMO, when working in a company/team/open-source-project it's just not worth the effort and risk (of over-use). You can do something like this instead (a little more verbose but more readable for humans and compilers): ``` { const {loadEventEnd, navigationStart} = window.performance.timing; console.log(loadEventEnd - navigationStart); } ```