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

158
Visualizações
How Prototypal inheritance works in reverse order

As you can see below is my code. I have created two objects halogen and balloon and given halogen property of sing and balloon property of read. I am calling halogen.read() it is getting read property but halogen is prototype for balloon but balloon is not prototype of halogen. How js is working under the hood??

const halogen = {
  sing: function() {
    console.log('I can sing');
  }
}

const balloon = new Object(halogen);

balloon.read = function() {
  console.log('I can read');
}

halogen.read();

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

0

When you call the Object constructor with an argument that isn't null or undefined, it doesn't create a new object (not even when you use new, surprisingly; spec link), it just converts the argument to object if it isn't already an object and returns it. Since halogen already refers to an object, there's no conversion required and you're basically doing balloon = halogen and they both end up referring to the same object.

You probably meant to use Object.create, which creates a new object and assigns the object argument you provide as the new object's prototype:

const halogen = {
    sing: function() {
        console.log("I can sing");
    },
};

const balloon = Object.create(halogen);

balloon.read = function() {
    console.log("I can read");
};

console.log(typeof halogen.read); // undefined
console.log(typeof balloon.read); // function
console.log(typeof balloon.sing); // 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