Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!