Say something about undefined
- Lingua principale
- JavaScript
- Stelle
- 148k
- Fork
- 26.6k
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
I find people treat `undefined` incorrectly a lot. It's a value, you can use it, test for it, etc, but people frequently use `typeof x == "undefined"` when they don't need to.
I made a [fork](https://github.com/ianb/javascript) with a big changeset of my own opinions, but here's the section I wrote about undefined:
---
``````
## undefined
- `undefined` is an object. You should use it as an object. You can test for it. For instance:
```javascript
// good
function pow(a, b) {
if (b === undefined) {
b = 1;
}
return Math.pow(a, b);
}
// bad
function pow(a, b) {
if (typeof b == "undefined") {
b = 1;
}
return Math.pow(a, b);
}
```
- *Only* use `typeof x == "undefined"` when the variable (`x`) may not be declared, and it would be an error to test `x === undefined`:
```javascript
if (typeof Module == "undefined") {
Module = {};
}
// But also okay, for browser-only code:
if (window.Module === undefined) {
Module = {};
}
```
Note that you can't use `window` in Node.js; if you think your code could be used in a server context you should use the first form.
``````
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Direzione di ricerca
Esamina prima il testo dell’issue e la sezione proposta su undefined nel fork collegato; non è indicato alcun file del repository né alcun test. Conferma le indicazioni preferite dal progetto per verificare undefined, quindi, se i maintainer sono d’accordo, aggiorna la documentazione pertinente della guida di stile e verifica che la formulazione finale sia coerente con le indicazioni circostanti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 30/100