airbnb / airbnb/javascript

Say something about undefined

Offen
#60 26 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
JavaScript
Sterne
148k
Forks
26.6k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Prüfe zuerst den Issue-Text und den vorgeschlagenen Abschnitt zu undefined im verknüpften Fork; keine Repository-Datei oder kein Test ist genannt. Bestätige die vom Projekt bevorzugte Vorgehensweise zur Prüfung auf undefined, aktualisiere anschließend die relevante Style-Guide-Dokumentation, falls die Maintainer zustimmen, und verifiziere, dass der endgültige Wortlaut mit den umgebenden Richtlinien konsistent ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript
Bereich
documentation
Issue-Typ
Dokumentation
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
30/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.