-1

I follow the instructions in https://stackoverflow.com/a/881556/2704265 to add a namespace, as below:

var theme1 = {
    document.addEventListener('DOMContentLoaded', function() {
    });
};

However, after that, I will get Uncaught SyntaxError: Unexpected token '.' which point to the '.' after 'document', why?

alancc
  • 487
  • 2
  • 24
  • 68
  • Object literals require key-value pairs. `document.addEventListener(...` is not a key-value pair. – CertainPerformance Oct 26 '21 at 02:40
  • @CertainPerformance, Thank you. Why removing the namespace will solve the issue? – alancc Oct 26 '21 at 02:44
  • When you type `theme1 = { document`. The next thing the parser is looking for is: another part of the identifier (i.e., `document1`); a whitespace character, followed by one of the remaining items (i.e., `document `); a colon (i.e., `document:`); a comma, if the identifier refers to a variable already defined (i.e., `document,`), an open parenthesis (i.e., `document(`). There are other symbols that can be used but generally, with a open curly brace, some whitespace and the identifier `document`, that's about the only valid things you can put. – Heretic Monkey Oct 26 '21 at 03:00

1 Answers1

0

Taking a look at the example given in the answer provided you have the following:

var yourNamespace = {

    foo: function() {
    },

    bar: function() {
    }
};

...

yourNamespace.foo();

The code you gave would need to look something more like this:

var themeNamespace = {
   domLoader: function() {
      document.addEventListener('DOMContentLoaded', function() {
    });
   },
}

Then run using themeNamespace.domLoader();

Max
  • 77
  • 6