1

I'd like to reference a get function as a Function object rather than as the value that it returns.

Normally i would be able to simply refer to the function without parenthesizes like so:

private function getFoo():int {
    return 0;
}

trace(getFoo); // traces function

But the whole point of get functions is that you can call the function without the parenthesizes, so i just get a return of 0 if i do this:

private function get foo():int {
    return 0;
}

trace(foo); // traces 0

Is there be any way at all to grab a reference to the foo function object?

Kris Welsh
  • 334
  • 2
  • 14
  • 2
    possible duplicate of [Referencing getter/setter functions in actionscript 3](http://stackoverflow.com/questions/1478923/referencing-getter-setter-functions-in-actionscript-3) – Andrey Popov May 22 '14 at 09:59
  • It would also be useful to be able to do this with set functions as well. – Kris Welsh May 22 '14 at 09:59

1 Answers1

2

Your first example gets a reference to the function (as it traces Function).

There is no way to get a reference to a getter, as getters are not simple functions, but a representation of a (custom) property of that object. They are not meant to work as a standard ones and so they are not meant to be referenced.

I cannot imagine why would you want to get a reference to that getter? And also, getters are not meant to be used only because you can skip those two symbols ()..

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
  • also duplication: http://stackoverflow.com/questions/1478923/referencing-getter-setter-functions-in-actionscript-3 – Andrey Popov May 22 '14 at 09:59
  • Thank you, I couldn't find the original question. – Kris Welsh May 22 '14 at 10:06
  • The original problem was with set function but the question flowed better when i talked about get functions, I've written a little class to record that a function was called (with a reference to the function), and i would love to be able to use it with set functions. – Kris Welsh May 22 '14 at 10:06
  • My understanding of the get and set keywords for accesors and mutators is that they provide the illusion of having a public variable, which makes the code using them look nicer, whilst still having the encapsulation of having a set and get function. – Kris Welsh May 22 '14 at 10:09
  • That's pretty interesting, I didn't know that get and set functions are changed to a different entity all together, i just assumed that it made the compiler automagicaly change stuff like `foo=0` to `setFoo(0)` – Kris Welsh May 22 '14 at 10:11
  • 1
    Well they are commonly used to provide limited access to properties (only getter but not setter); they can be very helpful when debugging and provide numerous (dis)advantages. You won't be able to track if they are called with your console, and I still don't understand why you would need getter and not a simply `getX()` function, which you can track.. It's not that ugly :) – Andrey Popov May 22 '14 at 10:55
  • That's how I was coding originally, I moved to get and set because that's how the library functions are built and I wanted my code to be consistent with the classes I was using. I think i'll probably move back to the more traditional technique. – Kris Welsh May 22 '14 at 11:23