- in Java, a package is a unique namespace for a group of classes
- packages conceptually contain classes like files in a folder, preventing naming conflicts
- e.g., suppose two people create global MessageDialog classes, stored in two .swfs:
_global.MessageDialog = function () {
// ...
}
- if one .swf loads into the same Flash Player as the other .swf, the second .swf's MessageDialog overwrites the first .swf's MessageDialog
- in ActionScript, there are no packages
- but we can prevent naming conflicts by defining each class on a uniquely named object, such as:
org.moock.ui.MessageDialog = function { ... }
com.yourdomain.MessageDialog = function { ... }
- now the MessageDialog classes don't conflict!
- but what about creating the objects: org, moock, and ui?
- we could do it manually:
if (!_global.org) {
_global.org = new Object();
}
- but it's more convenient to use a custom function, AsSetupPackage():
_global.AsSetupPackage = function (path) {
var a = path.split('.');
var o = _global;
for (var i = 0; i < a.length; i++) {
var name = a[i];
if (o[name] == undefined) {
o[name] = new Object( );
}
o = o[name];
}
}
AsSetupPackage("org.moock.ui");
org.moock.ui.MessageDialog = function { ... }