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

168
Views
shorthand way to reference `this` propeties?

I have a class

class Foo {
  constructor(x, y) {
    this.x = x
    this.y = y
  Bar() {
    console.log(this.x, this.y)
}

I recall seeing a method of referring to this. variables without having to specify this. Something like

class Foo {
  constructor(x,y) {
    this.x = x
    this.y = y
  Bar() {
    x,y = [this] // something like this
    console.log(x, y) // equivalent to this.x, this.y
}

I desire this because I have dozens of statements that call methods on this.ctx - like this.ctx.rect() - and I don't want to have to have this. prepended to all of them for the sake of readability.

Is there a syntax for this?

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

0

You can deconstruct this like any JavaScript object,

Bar() {
    const {x, y} = this;
}

I do not recommend doing this however, it seems like it would not scale well as a technique.

about 4 years ago · Juan Pablo Isaza Report

0

Objects (including arrays and functions) are handled by reference, meaning that if you write

let a = this.ctx;

then a points to the same object as ctx and you can do a.rect().

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!