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

306
Visualizações
What happens to existing objects when you declare prototype inheritance?

//debugger;

function User(email,name) {
    this.email = email;
    this.name = name;
    this.online = false;
}

User.prototype.login = function() {
    this.online = true;
    console.log('this.name is now online');
}

function Admin(...args) {
    User.apply(this, args);
    this.title = 'admin';
}

const admin1 = new Admin('admin@admin.com', 'josh');

Admin.prototype.deleteUser = function() {
    console.log('deleted user');
}

Admin.prototype = Object.create(User.prototype);

const admin2 = new Admin('admin2@admin.com', 'barry');

console.log(admin1); //admin1 will have the deleteUser() method in its prototype, and DIDN'T update after Admin inherited the User prototype 

console.log(admin2); //admin1 will have the login() method and NOT deleteUser()

Why did admin1's prototype update when I made the deleteUser() method, but why did it not update at all when I inherited User.prototype?

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

0

I realized that when I declared deleteUser() I was simply adding onto the prototype object of Admin, but when I inherited User's prototype I completely reassigned Admin's prototype. For example, if I declared x = {a:1} I could do x.b = 2 and then x is now {a:1, b:2}. BUT if I do x = {a:1} and then right after do x = {b:2}, it completely replaces the value of x, not add onto it.

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