3

I have the following Javascript file:

class Coo {
    constructor() {
    }
}

function foo() {
}

Now if I load this into a browser (with a script tag), there is now a function window.foo defined. However, window.Coo is undefined.

So how does the scope of class work and how is it different from function?

In other words: Where do classes live?

1
  • 2
    See the linked questions' answers. Global class definitions don't add properties to the global object (which is, loosely speaking, window on browsers). This is also true of let and const declarations. They create globals, but not properties on the global object. Older-style declarations (function, var) at global scope do create properties on the global object. When the new forms were being added, TC39 (the steering committee for JS) were trying to reduce the exposure of things. Sep 22, 2019 at 10:13

0

Browse other questions tagged or ask your own question.