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

179
Views
javascript static class detect which method call previous

could you help me! In the emit method how to check they call room method previous or not.
In the two cases below:

// case 1: A.room('aa').emit('a1', 'aaa')
// case 2: A.emit('a2', 'aaa')

this is a class A

class A {
  static room(r) {
    this.r = r
    return this
  }
  static emit(event, data) {
    //todo
    console.log('this.r', this.r, {event, data})
  }
}

Thank for your time!

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

0

You need to store flow values like r separately for each call, which is not reasonable aim for static classes because you use same same class again and again, instead of separate instanses. Possible solutions:

1. No longer static:

class A {
    room(r) {
      this.r = r
      return this
    }
    emit(event, data) {
      console.log('this.r', this.r, {event, data})
    }
}
new A().room('aa').emit('a1', 'aaa') // r = 'aa'
new A().emit('a2', 'aaa')            // r = undefined

2. Return instances with own scopes (A keeps static):

class A {
    static room(r) {
      return new B(r)
    }
    static emit(...args) {
      return new B().emit(...args)
    }
}
class B {
    constructor(r) {
        this.r = r
    }
    emit(event, data) {
        console.log('this.r', this.r, {event, data})
        return this
    }
}
A.room('aa').emit('a1', 'aaa') // r = 'aa'
A.emit('a2', 'aaa')            // r = undefined

3. Delegate logic to non-static class (A keeps static):

class B {
    room(r) {
      this.r = r
      return this
    }
    emit(event, data) {
      console.log('this.r', this.r, {event, data})
    }
}
class A {
    static room(...args) {
        return new B().room(...args);
    }
    static emit(...args) {
        return new B().emit(...args);
    }
}
A.room('aa').emit('a1', 'aaa') // r = 'aa'
A.emit('a2', 'aaa')            // r = undefined

... and so on.

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!