Home > Net >  JavaScript class names are the properties of what object?
JavaScript class names are the properties of what object?

Time:05-04

This is not about CSS classes and the answer to my question is near impossible to find because of the word "class" in connection with JavaScript usually relates to HTML CSS classes. With that no more words. Just console dialog:

> function Foo() {}
< undefined
> window.Foo
< function Foo() {}
> class Bar {}
< undefined
> window.Bar
< undefined
> Foo
< f Foo {}
> Bar
< class Bar() {}
> window.Zoo
< undefined
> Baz
x Uncaught ReferenceError: Baz is not defined

Before there was a class keyword we defined classes as functions and those names ended up properties of the window object (at least in the browser). Where are the class names now? How can I delete a class as I can delete window.Foo? How can I test a class has been defined without getting a Name is not defined error?

CodePudding user response:

First and foremost, we're just talking about variables. You're conflating the behavior of global variables with classes. The new-ish class keyword is just syntactic sugar around a variable declaration with let, the same way function was (for our purposes) equivalent to var. Classes have no relevance to your question. The same way that global variables declared with let no longer appear as properties of the global object, so to do variables declared with class.

Before there was a class keyword we defined classes as functions and those names ended up properties of the window object

No, only variables (all variables, not just "classes") defined at global scope with var or function wound up as properties on the global object. A "class" (ie constructor function) or any other variable declared in any other scope would be bound to that scope and would not appear on the global object:

function Foo() { }
window.Foo // function Foo() { }
(function () { function Bar() { } })();
window.Bar // undefined

Where are the class names now?

Where they have always been, whether declared with function or var or let or class: The scope in which they were declared. Constructors created with class are just variables in whatever scope you're in, just like variables.

{
  class Foo {}
} // Foo is gone

How can I delete a class as I can delete window.Foo

You wouldn't do this. You'd just let it go out of scope, or set the variable to undefined if you really want to reuse the variable for some reason:

class Foo { }
Foo = undefined;

How can I test a class has been defined without getting a Name is not defined error?

Like you always should have, for all variables, regardless of how they were declared: typeof. You never should have been checking for properties in window as this only worked for global variables.

typeof Foo // "undefined"
class Foo { }
typeof Foo // "function"
  • Related