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

130
Visualizações
Detect object literal being a serialization of a class

I am looking for a way to detect if an object literal is in fact a "non-instantiated" object of a certain class. I hope it will be more clear with an example:

class Range{
    from: number;
    to: number;

    constructor(from: number | Range, to?: number){

        if (from instanceof Range)
            Object.assign(this, from)
        else{
            this.from = from;
            this.to = to;
        }
    }

    average(){return (this.to -this.from) / 2;}
}


let one = new Range(1, 20)
let copyOfOne = structuredClone(one);   // This generates an object literal (or kind of ?), the method "average" is not in the prototype
let two = new Range(copyOfOne)          // Constructor won't detect the parmeter as instanceof Range

Is there a way to know if an object literal is in fact a "non-instantiated instance" of a certain class? (of course I could check if the object has the class properties, here is simple but with more complex objects would be impractical)

about 4 years ago · Juan Pablo Isaza
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