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

224
Visualizações
Function parameter precedence versus rest parameter

Below there are two object constructor Newobj is parent one and I want to create exact same object constructor with some extra parameter.

issue is: I want to use rest parameter but rest parameter can only send in last as argument, I also want some extra argument like home but (child2.home) is giving me ana1 output. it should give me 45. it will give name as jersey and address as 45.

//first constructor 
function Newobj(name, adress) {
  this.name = name;
  this.adress = adress;
  this.fullA = function () {
    return this.name + " " + this.adress;
  };
}

//second constructor using apply method for inheritance
function Newobj1(home, ...args) {
  Newobj.apply(this, args);
  this.home = home;
}

const child23 = new Newobj("ana", "newport");
const child2 =  new Newobj1("ana1", "jersey", "45");

console.log({ child23, child2 });
.as-console-wrapper { min-height: 100%!important; top: 0; }

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

0

  function newobj(name, address) {
      this.name = name;
      this.address = address;
      this.fullA = function () {
          return this.name + " " + this.adress;
      };
  }

  //second constructor using apply method for inheritance
  function newobj1(...args) {
      const home = args[2] || "";
      newobj.apply(this, args);
      this.home = home;
  }

  const child23 = new newobj("ana", "newport");
  const child2 = new newobj1("ana1", "jersey", "45");
  const child3 = new newobj1("ana1", "jersey");
  console.log(child2);
  console.log(child23);
  console.log(child3);

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