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

174
Visualizações
Difference between two function placements

What would be the difference between the following two ways of attaching a method(?) to a parent function:

var Person = function(first, last) {
    this.first = first;
    this.last = last;
};
Person.prototype.personMethod = function() {
    // ...
};

And:

var Person = function(first, last) {
    this.first = first;
    this.last = last;
    this.personMethod = function() {
        // ...
    };
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In the first approach, the method is put onto the prototype. Every instance will share the same personMethod because every instance inherits from Person.prototype.

var Person = function(first, last) {
    this.first = first;
    this.last = last;
};
Person.prototype.personMethod = function() {
    // ...
};

const p1 = new Person();
const p2 = new Person();
console.log(p1.personMethod === p2.personMethod);
console.log(p1.hasOwnProperty('personMethod'));
console.log(Object.getPrototypeOf(p1).hasOwnProperty('personMethod'));
console.log(Object.getPrototypeOf(p1) === Person.prototype);

In the second approach, every instance has its own separate method, and it's not on the prototype.

var Person = function(first, last) {
    this.first = first;
    this.last = last;
    this.personMethod = function() {
        // ...
    };
}

const p1 = new Person();
const p2 = new Person();
console.log(p1.personMethod === p2.personMethod);
console.log(p1.hasOwnProperty('personMethod'));

In modern JavaScript, it's usually preferable to use a class instead, which puts methods onto the prototype.

class Person {
  constructor(first, last) {
    this.first = first;
    this.last = last;
  }
  personMethod(){}
};

const p1 = new Person();
const p2 = new Person();
console.log(p1.personMethod === p2.personMethod);
console.log(p1.hasOwnProperty('personMethod'));
console.log(Object.getPrototypeOf(p1).hasOwnProperty('personMethod'));
console.log(Object.getPrototypeOf(p1) === Person.prototype);

Assigning the function inside the constructor can be useful if you want the function to close over a variable only scoped to the constructor, which wouldn't work for methods if the variable isn't set to the instance.

var Person = function(first, last) {
    // say we don't want to assign these arguments to the instance
    // but we can still do
    this.getName = () => first + ' ' + last;
}

const p = new Person('John', 'Doe');
console.log(p.getName());

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