Getter functions allow obj.meth
syntax instead of obj.meth()
, I'd like to create an anonymous one of these to return from another function. function get ():Object { }
is invalid syntax. I don't suppose Flex offers an easy way to get this functionality, if it's even possible?
views:
37answers:
2
+1
A:
If I understand, you want to create an anonymous getter function? why? What possible purpose would this serve?
If you want to create properties on the fly, you can do use an Object or Dictionary:
myObject['newProperty'] = something;
The 'newProperty' could be anything you want including another variable.
www.Flextras.com
2010-08-26 21:07:03
Because a function can do a lot more than just return or contain a value, and unless I've been deceived Actionscript's anonymous functions are proper closures and can use outside contexts, making them considerably powerful. There should be a way to dynamically create getter functions since you can dynamically create normal functions, even dynamically add them to classes. In Python you can create a descriptor object by overloading `__get__()`, and while AS doesn't have operator overloading it doesn't struggle from that.
Kevin
2010-08-26 23:01:06
Yes, a function can do a lot more than return a value. Yes, anonymous functions can be used as proper closures. get methods cannot be called like a regular function, though. They must be called using object property notation. Traditional methods do not have that limitation. Why can't you use a "regular" anonymous function? Why do you insist it must be a get function?
www.Flextras.com
2010-08-27 02:30:20
+3
A:
Hi!
You can override Object's default behavior by extending flash.utils.Proxy.
I made a very simple online code sample @ wonderfl.net
http://wonderfl.net/c/ngtC
I implemented 'setAnonymousGetter' to register an anonymous getter function.
In 'getProperty', if the property is an anonymous getter, executes 'apply' and return its result, otherwise returns the value of the property.
9re
2010-08-27 02:51:27
Very creative approach. But, it seems like a lot more work than just adding new properties as needed like this myObject['newProperty'].
www.Flextras.com
2010-08-27 12:00:04