Feb 24, 2009

Namespaces in AS3

AS3 gives you advantages of defining user defined namespaces. AS2 already started namespaces with public and private however these were system defined. AS3 gives you flexibility to define user-defined namespaces. Those, who are familiar with OO languages like C++, Java and markup languages like XML must be familiar with user-defined namespaces.
Thus now there are two types of namespaces in AS3:

  1. Predefined namespaces: public, private, protected, internal, static
  2. User-defined namespaces
The flow of using user-defined namespaces could be:
  • Define a namespace:
  • Assign the namespace to a method/property.
  • Call the method/property using its namespace.
Note: Namespaces are declared using 'namespace' Keyword.

package
{
 // define the namespace like this:
 public namespace my_public;


 //or like this:
 public my_public namespace;
 public class MyClass
 {
  // assign the namespace to a method
  my_public function myFunc()
  {
   //do something here.
  }

  // call the method using its namespace either this way:
  use namespace my_public;
  myFunc();

  // or this way:
  my_public::myFunc();
 }
}

No comments:

Post a Comment

I love comments.