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

417
Visualizações
What does 'new' do to modify the `this` object in a function?

What does the new keyword do that allows the following binding on the this object:

function Person(first, last) {
    this.first = first;
    this.last = last;
}
Person.prototype.greet = function() {
    console.log(`Hello, ${this.first} ${this.last}.`);
};
let p = new Person('Tom', 'Jones');
p.greet();

Whereas without the new keyword, this.first would raise an error since it is undefined. Conceptually speaking, what is new doing here in how it modifies the function?

Without new:

function Person(first, last) {
    this.first = first;
    this.last = last;
}
Person('Tom', 'Jones');
// TypeError: Cannot set property 'first' of undefined


And without using new seems the following is a crude way to simulate it:

function Person(first, last) {
    this.first = first;
    this.last = last;
    return this;
}
Person.prototype.greet = function() {
    console.log(`Hello, ${this.first} ${this.last}.`);
};

Person.prototype.greet.call(Person.call({}, 'Tom', 'Jones'));

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