Package-level function closures are very useful for creating generalized functionality which does not require a class (static methods) or instance of a class (instance methods).
Unlike static and instance methods package-level function closures are not associated with a class or instance of a class but rather with a package. There are no syntactical differences between package-level functions and static or instance methods.
Package-level functions are for the most part utility functions; for instance the flash.utils package contains a number of package-level functions, the most common of which are describeType(), getDefintionByName(), getTimer() and so forth.
Package-level function closures are created by defining a function directly inside the body of a package (where class and interfaces are defined), as can be seen in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.ericfeminella.display { import flash.display.Bitmap; import flash.display.IBitmapDrawable; import mx.graphics.ImageSnapshot; public function createSnapShot(target:IBitmapDrawable) : Bitmap { return new Bitmap( ImageSnapshot.captureBitmapData( target ) ); } } |
Calling a package level function is straightforward, simply import the function just as you would a class or interface and then invoke the function directly…
1 2 3 4 5 6 7 | // import package function import com.ericfeminella.display.createSnapShot; // once imported the function can be invoked createSnapShot( this ); |
Typically you will find that most functionality can be grouped to a Class or an instance object, however on occasion you may identify specific functionality which is common to packaged functionality as opposed to a specific object, and in these cases utilizing package-level functions is a great option.
Excelent Eric! I love to see this details described. Keep this coming please.
Hey Eric,
Great post. Is the Singleton Enforcer an example of this?
Kind regards,
Sean
Hey Sean,
Are you referring to the SingletonManager?
http://code.ericfeminella.com/classes/as3/SingletonManager.as.html
Thanks,
Eric
Pretty cool. Thanks for the information.
does this work in flex 3? or is it just a flex 2 thing?
Hey Sam,
This is specific to ActionScript 3.0 so it should work in Flex 2 and 3 as well as AIR and Flash.
– Eric
This doesn’t seem to be working for me. I am using Flex 3. I created a function as you said…and I can import it…and the Flex code hinting picks it up and everything (just like the built in functions getTimer() etc…) however after I type in a function or whatever it gives me
“Definition com.cheezeworld.com.utils:stripWhitespace could not be found.”
I don’t understand? meh…
Great post, like the man sai – keep them coming.
Another great topic might be inner or package level private classes that are only available inside a package.
Please explain why this is a closure. To me it just looks like a normal top-level function. CLosure implies some specific things about variable binding.
Hi Colby,
All u need to do is, just name the class file with the same function name.
This looks like a global function masquerading under a different name.