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

176
Views
javascript this keyword rules

Example No:1

function foo() { 
    console.log(this.bar)
}
var bar = "bar1"
foo();

Example No:2

function foo() { 
    console.log(this.bar)
}
var o2 = {foo:foo}
console.log(o2.foo())

Example No:1 will print bar1 where as example no:2 will print undefined. It seems pretty counter intuitive to me that in the first scenario javascript will look at the global scope where as in the second scenario it will not use scoping rules.

Why does the language work this way or is this some arbitrary rule that I need to remember(whenever there is an object involved javascript engine will not consider scoping rules)

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!