Understanding the ActionScript Language > About custom objects > Creating a custom object

 

Creating a custom object

To create a custom object, you define a constructor function. A constructor function is always given the same name as the type of object it creates. You can use the keyword this inside the body of the constructor function to refer to the object that the constructor creates; when you call a constructor function, Flash passes it this as a hidden parameter. For example, the following is a constructor function that creates a circle with the property radius:

function Circle(radius) {
	this.radius = radius;
}

After you define the constructor function you must create a new instance of the object. Use the new operator before the name of the constructor function and assign the new instance a variable name. For example, the following code uses the new operator to create a new Circle object with a radius of 5, and assigns it to the variable myCircle:

myCircle = new Circle(5);

Note: An object has the same scope as the variable to which it is assigned. See Scoping a variable.

For more information about creating and using objects, see About built-in objects.