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

88
Views
javascript using this in a child method

Method:

a = {
  foo: 1,
  bar() {
    return this.foo + 1
  },
  lol: {
    baz() {
      return this.foo
    }
  }
}

a.bar() this which refers to a which is what I want. I'm looking for a way for the child method inside a.lol.baz to also have this refer to a. Is there anyway to do it?

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

0

You can't refer to it directly. But you can bind the function to the object explicitly, then this will be available.

a = {
  foo: 1,
  bar() {
    return this.foo + 1
  },
  lol: {}
}

a.lol.baz = (function() {
  return this.foo
}).bind(a);

console.log(a.lol.baz());

about 4 years ago · Juan Pablo Isaza Report

0

you can use a.lol.baz.call(a) function

a = {
  foo: 1,
  bar() {
    return this.foo + 1
  },
  lol: {
    baz() {

      return this.foo
    }
  }
}
var res=a.lol.baz.call(a)
alert(res)

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!