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

197
Views
Is it possible to call a function in an object that references itself?

I have an object that looks something like this:

const state = []

const spells = [
  {
    name: 'test',
    channelDuration: 1000,
    castDuration: 1000,
    cast: () => {
     state.push(THIS) // <--
    }
  }, {...}, {...}
]

When a player uses a spell, that spell gets stored into the game state and then waits for the channelDuration and castDuration to pass.

Once able to cast, it looks into the state and calls the cast method:

State.quest.bossFightState[USER].spell.cast()

state = [Spell]

On said cast, i need to push the spell into the a new array called statuses. However, i can't reference the object within the cast method without using some additional helper functions to grab the specific spell. And I dont want to pass add a parameter spell into the cast method because that just seems dumb.

Any ideas? Or is a helper method the way to go here.

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

0

One option would be to use a function or method instead of an arrow function, so that .cast will have a this referring to the outer object.

const state = []

const spells = [
  {
    name: 'test',
    cast() {
     state.push(this)
    }
  }
]

spells[0].cast();
console.log(state);

about 4 years ago · Juan Pablo Isaza Report

0

I'm surprised nobody else caught this... You're using this inside of an arrow function. The this keyword acts differently in arrow functions - read more about that here

const state = [];

const spells = [
    {
        name: 'test',
        cast: function () {
            state.push(this);
        },
    },
];

spells[0].cast();
console.log(state);

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!