0

I require your assistance to help me understand how JS engines may work.

Context

Lately, I've been working with JS modules and I used the same pattern every time :

A specific_html_file.html :

<head>
    <script type="module" src="javascript/import_yaml.js"></script>
</head>

A import_yaml.js :

import {DomFuncs} from './import_js/dom_functions.js';
window.addEventListener (`DOMContentLoaded`, DomFuncs.initImportPage);

A dom_functions.js :

import {SpecFuncs} from './specific_functions.js';   // Used in script

export class DomFuncs {
     // Different DOM related functions
}

Question

This worked pretty well for the two first pages I wrote but the last logs an error in the console.

"Uncaught SyntaxError: Unexpected token {" on Chrome,

"SyntaxError: import declarations may only appear at top level of a module" on Firefox.

But in the end, all works perfectly fine so :

Why does the engine prints an error but works anyway in this particular file ? Why doesn't it react the same way in the first two pages ?

Thanks in advance,

A junior dev trying to understand as many things as possible

Bhoomi Patel
  • 777
  • 10
  • 32
Jleu
  • 15
  • 7
  • `\`use strict\`;`?! That a) doesn't work because directive prologues need to be string literals b) isn't necessary because ES6 modules are always strict mode anyway – Bergi Dec 03 '18 at 11:01
  • Did you forget `type="module"` in the last page? Please post the exact code of the page where it is not working so that we can reproduce the issue. – Bergi Dec 03 '18 at 11:03
  • @Bergi, removed, doesn't change anything :/ I edited to show the actual code that isn't working, does i thelp to identify the problem ? – Jleu Dec 03 '18 at 11:15
  • I don't see anything wrong with the syntax, no. – Bergi Dec 03 '18 at 11:22
  • (However it looks like you are [abusing `class` for creating an object of static functions](https://stackoverflow.com/a/29895235/1048572)) – Bergi Dec 03 '18 at 11:23
  • @Bergi : I agree and in fact, everything going ok seems to indicate that it is indeed correct. Nonetheless, it still raises an error :/ And about the "too much static functions in the class", looks like it. I'll talk about it with the colleague that asked me to do it this way (But before it had to be sliced in different files) if it's still relevant to keep it this way. Thanks ! – Jleu Dec 03 '18 at 12:39

0 Answers0