Error about using var

Aperta
#1 0 commenti 1 reazione 1 assegnatario Vedi su GitHub

@alhassy ci sta già lavorando.

Dal 27/1/2021.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

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...

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.

Lingua principale
Nessun dato sulla lingua
Stelle
221
Fork
23
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di alhassy/JavaScriptCheatSheet

Tutte le issue di alhassy/JavaScriptCheatSheet

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.