Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

makeNamespace.as

Name: makeNamespace [MX] creates a namespace in _global (or your spec. scope)
Author: senocular: www.senocular.com
Date: 1899-12-31T00:36:53.400
Documentation:
function MAKENAMESPACE: helpful in developing a unique object
to maintain your component (or any other) classes in.

myNamespace = makeNamespace("path",targ);


Arguments:
- path: a string path in dot syntax specifying the path of which the object or namespace
is to be created.
- targ: (optional) the target object for the path.  If target isnt specified,
_global is assumed

Returns:
- returns the object or namespace created from running the function.  This object also has a
parameter str_path which is a string representation of the path you passed in only seperated
by underscores instead of periods. 

Example:
// In component def:
#initclip
#include "makeNamespace.as"
proj = makeNamespace("senocular.pixel.ProjectFido");
proj.myClass = function(){
	this.name = "myClass_name";
}
proj.myClass.prototype.toString = function(){
	return this.name;
}
Object.registerClass(proj.str_path +"_myClass", proj.myClass);
#endinitclip


// in the main timeline:
comp = senocular.pixel.ProjectFido;	// component scope saved to comp
myObj = new comp.myClass();		// make new class
trace(myObj);			// "myClass_name"	

1
2
3
4
5
6
7
8
9
10
if (!makeNamespace){
	var makeNamespace = function(path, targ){
		path=path.split(".");
		if (!targ) targ=_global;
		var L=path.length;
		for(var i=0; i<L; i++) if (!targ[path[i]]) targ = targ[path[i]] = {};
		targ.str_path = path.join("_");
		return targ;
	}
}