Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
JavaScript class names are the properties of what object?

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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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"
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda