Putting functions inside objects at runtime

Putting functions inside objects at runtime

All functions within Flex are objects of type Function. This means you can do fun things with them like pass them around within your application. You can even pass a private function to another class and have it invoke it for you. There are lots of examples around on the net where people use this functionality to define call-back functions for asynchronous methods etc.

The documentation for the Function class says that the ‘call’ function (which invokes the function represented by this object) takes as its first parameter the ‘thisObject’. The ‘thisObject’ being the object which is referred to by the keyword ‘this’ from inside the function.


Parameters
thisObject:Object — An object that specifies the value of thisObject within the function body.

myObject.myMethod.call(myOtherObject, 1, 2, 3);


The first thing I thought of when I read that was, surely then you could make the ‘thisObject’ something other than the object which originally held the function. If that were possible, there are a lot of ways in which this could go wrong, but there are also a lot of interesting use cases for such functionality. On my current project at work I got a chance to play with this functionality, and I can confirm that it does work, but it’s a little trickier than the docs imply.

If you declare a function in the normal way inside a class, you can create an object of that class type and get a reference to that function. If you then try to invoke that function with a ‘thisObject’ of another class type, the object you pass in will be ignored and the original object will be used as the ‘thisObject’. This isn’t explained by the documentation at all, and for a while I thought that the documentation was wrong.

It turns out, if you want to be able to dynamically set the ‘thisObject’ to be used within a function, you have to declare that functions outside of any object. Because this function isn’t declared inside an object any more, you can set the ‘thisObject’ at runtime without any problems.  It’s possible some people won’t know the syntax to declare a function outside of a class, and it’s a bit hard for me to explain in words exactly what you need to do to take advantage of this functionality, so I built a small demo application to help explain it.

DEMO APPLICATION
Right click on the application to view the source code and see how it’s done.

The possibilities of what you can do with this functionality are endless, but in the particular project which lead me to discover this I am going to use this functionality to make highly flexible and reusable components. Where in the past I would have to code all of the functionality I wanted into a component up front, and then configure the component somehow to perform how I wanted it to. Now I can simply pass a function into my component and have it execute as if it had been developed as part of that class originally.

Many thanks to Alex for helping me figure this out.

Share

0 条评论

留下评论