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

160
Visualizações
Are private fields of subclasses of built-in classes not accesible inside overriden methods?

I have the following class that extends the built in data type Set. It adds a little bit of extra functionality by checking that every element that wants to be added to the set conforms to the type specified as the first parameter of the constructor. It does this silly "type checking" through the typeof operator. I save the type of the Set in a private field. If I try to override the .add() method so that it also does this little type checking, I get an error stating Uncaught TypeError: can't access private field or method: object is not the right class

class TypedSet extends Set {
    #type;
    constructor(type, elements) {
        if (elements && !elements.every(e => typeof e === type)) throw new TypeError(`Not all elements conform to type t->${type}`)
        super(elements)
        this.#type = type
    }
    add(e) {
        return typeof e === this.#type
            ? super.add(e)
            : new TypeError(`Type of element e does not conform to type t: ${this.#type}`)
    }
}

const s = new TypedSet('string', ['hello', 'world'])
s.add('!')

Why do I get that error if i try to access the private field -- the one I declared in the subclass -- in the overriden .add() method ? If I rename the .add() method to something like .typeAdd() referencing the private field doesn't throw an error

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

0

The problem is that super(elements) will call the add method to add the elements to the set1. At that point, your subclass constructor did not yet create the field (this.#type = type).

As a workaround, you can use

class TypedSet extends Set {
    #type;
    constructor(type, elements) {
        super()
        this.#type = type
        for (const e of elements ?? []) {
            if (typeof e !== type) throw new TypeError(`Not all elements conform to type t->${type}`)
            super.add(e) // or this.add(e)
        }
    }
    add(e) {
        return typeof e === this.#type
            ? super.add(e)
            : new TypeError(`Type of element e does not conform to type t: ${this.#type}`)
    }
}

const s = new TypedSet('string', ['hello', 'world'])
s.add('!')

1: Calling an overrideable method from the constructor is usually considered an antipattern, for exactly the reason that your use case doesn't work. No idea why ECMAScript did specify this regardless. On the other hand, it's also not recommended - even if possible - to extend builtin classes, there are too many surprises such as this. Composition is usually a better approach.

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