Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

79
Vistas
Can someone help me to sort out why in the current scenario this behave like this

Why does this behaves so weird (my understanding is wrong may be ;() . In the below code i have taken two scenario -> 1) am trying to add the entire personPrototype Object as a prototype of Person and 2) am trying to add a specific method of personPrototype (greet method) to the prototype of Person.

In both cases am getting value of this keyword differently why is it so ? can someone help me out. I have added both scenario in the below code and commented out my result as well.Am really interested to know is work of this keyword in current scenario and not the fix.

const personPrototype = {
    greet() {
      console.log(this)
      console.log(`hello, my name is ${this.name}!`);
    }
  }
  
  function Person(name) {
    this.name = name;
  }




  var p1=new Person("Sam")
  

Person.prototype.test=personPrototype.greet;
console.log(p1.test()) //prints Sam //this in greet method is Person obj

Person.prototype.test=personPrototype;
console.log(p1.test.greet()) //prints undefined //this in greet method is greet obj
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda