alhassy / alhassy/JavaScriptCheatSheet

Error about using var

Đang mở
#1 0 bình luận 1 reaction 1 người được giao Được @alhassy nhận Xem trên GitHub
Ngôn ngữ chính
Không có dữ liệu ngôn ngữ
Star
221
Fork
23
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

> In the same way, for the same purpose, we may use `var` but it has undesirable properties; e.g., its declarations are in the global scope and no error is raised using `var x = ⋯` if `x` is already declared.

This is at the end of the _Types_ section. First bullet point of three.

The part that says _"its declarations are in the global scope"_ is incorrect.

Later, in the _Scope and Statements_ section, you say...

>`let`, `const` declare local bindings; `var` always makes global ones!

Which again is incorrect. You can try this...

```javascript
function f() {
var x = 1;
}
console.log(x); // => Uncaught ReferenceError: x is not defined
```

...and see that `x` is **not** defined in the global scope. It will be local to the `f` function.

The difference about `var` and `let` in what regards scope is:

- `var` bindings reach either _function scope_ or the _global scope_. `let` bindings can also be limited to _block scope_.
- `var` declarations are hoisted to the start of the scope. `let` bindings are not (*)

(*) To be more precise they are but have a mechanism that prevents usage before declaration and will throw an error, while `var` won't.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.