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

232
Views
How exactly does javascript set context for objects created from constructor function using the new keyword

Consider the following constructor function having a method logging the context

function Foo() {
  this.logContext1 = () => console.log('logContext1', this);
}

let foo = new Foo();

foo.logContext2 = () => console.log('logContext2', this);

foo.logContext1();
foo.logContext2();

logContext1 has context (this) set to the Foo object created by new,

whereas logContext2 has context (this) set to the window object

Considering both are arrow functions, why do we see this difference?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

this is defined where the arrow functions are declared:

  • () => console.log('logContext1', this) is declared in the constructor, this is the object being created in that context
  • () => console.log('logContext2', this) is declared on top-level, this is window in that context (or undefined in strict mode)
about 4 years ago · Juan Pablo Isaza Report
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!