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

147
Views
Why is the 'this' value different in Javascript?
var obj = {
    a: 2,
}
var a = 3;

const func = () => {
    console.log(this.a);
}

function test() {
    setTimeout(func, 100);
}

function test2() {
    setTimeout(() => {
        console.log(this.a);
    }, 100);
}
test.call(obj); //3
test2.call(obj); //2

It seemed that the test and test2 are the same, but the returned result is different, what's the problem with 'this'?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

with test arrow function, this will refer to the window object, and as var a = 3, the a will be assigned as a property to the window, it will be like

window.a

But in test2 this will refer to the obj object.

about 4 years ago · Santiago Trujillo 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!