addyosmani / addyosmani/es6-equivalents-in-es5

Incorrect module import

Offen
#36 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug
Vorherrschende Sprache
Keine Sprachdaten
Sterne
2.5k
Forks
164
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

I believe the module docs are incorrect. Based on this `lib/math.js`

``` javascript
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
```

The docs say we can import like this

``` javascript
import math from 'lib/math';
console.log('2π = ' + math.sum(math.pi, math.pi));
```

But I think this is incorrect. In this case math is `undefined` because there is no default export. The correct import would be:

``` javascript
import * as math from 'lib/math';
```

or if you wanted to just destructure:

``` javascript
import { pi, sum } from Math;
```

I made this mistake today when dealing with code that was mixed with requires and import. These two statements below look equivalent because they similar structure which I think leads to the confusion:

``` javascript
var math = require('lib/math');
import math from 'lib/math';
```

There's a great reference here: http://www.2ality.com/2014/09/es6-modules-final.html for anyone who happens to come across this issue.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

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