1

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Very creative approach. But, it seems like a lot more work than just adding new properties as needed like this myObject['newProperty'].
1

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.

2 Comments

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.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.