logoalt Hacker News

zamadatixyesterday at 6:12 PM3 repliesview on HN

On a phone at the moment so I can't try it out, but in regards to this "stricter mode" it says global variables must be declared with var. I can't tell if that means that's just the only way to declare a global or if not declaring var makes it scoped in this mode. Based on not finding anything skimming through the examples, I assume the former?


Replies

lioetersyesterday at 6:35 PM

I'm guessing the use of undeclared variables result in an error, instead of implicitly creating a global variable.

MobiusHorizonsyesterday at 8:36 PM

it also talks about the global object not being a place to add properties. So how you might do `window.foo = ...` or `globalThis.foo = ...` to make something from the local context into a global object. in this dialect I guess you would have to reserve any global objects you wanted to set with a `var` and then set them by reference eg

    // global. initialized by SomeConstructor
    var fooInstance

    class SomeConstructor {
       constructor(...) {
          fooInstance = this;
       }
       static getInstance(...) {
          if (fooInstance != null) return fooInstance;
          return new SomeConstructor(...);
       }
    }
frabertyesterday at 6:32 PM

I think it means you can't assign to unbounded names, you must either declare with var for global, or let/const for local