#1975 Accessor Methods in JS

SlimerDude Wed 25 Jul 2012

If I define a field in a class thus:

Int x := 0 {
  set {
    &x = it
    // other stuff
  }
}

and somewhere in the class call

x += 3

then in javascript I get an

Uncaught ReferenceError: Invalid left-hand side in assignment

The generated JS is:

this.x() = fan.sys.Int.plus(this.x(),3);

which shows it's trying to assign the computation to the accessor method. What may I do instead?

SlimerDude Wed 25 Jul 2012

Ah, okay... I can use:

&x += 3
otherStuff()

and pull the // other stuff out into its own method. It'd still be easier if x += 3 worked though!

KevinKelley Wed 25 Jul 2012

does x = x + 3 work in place of x += 3 ? I seem to remember a JS issue with the += shorthands...

SlimerDude Wed 25 Jul 2012

Naa, I tried that. x = x + 3 generates the same JS. this.x() = ...

andy Thu 26 Jul 2012

What version of Fantom are you running? I believe that was fixed in 1.0.62. But I have verified it works in 1.0.63.

SlimerDude Thu 26 Jul 2012

I was under the impression it was 1.0.63 - but F4 may have been doing some 1.0.60 stuff under the covers. I'll re-check.

SlimerDude Sat 4 Aug 2012

Yep, with my new shiny Fantom 1.0.63 F4, it all works fine.

Login or Signup to reply.