Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#const" rel="nofollow"><strong><code>const</code></strong></a></p> <p>A constant is constant. That means it does not change, because it is constant. The idea behind a constant is that you can be assured its value does not change during runtime. If you try to assign a new value to a constant, you will get an error like this:</p> <blockquote> <p><code>1049: Illegal assignment to a variable specified as constant.</code></p> </blockquote> <p>In your example, <code>RADIUS</code> is not re-calculated. In fact, that behaviour doesn't occur when you access any variable - unless you are accessing it via a getter that needs to calculate the result before returning it. An example of this might be a getter that returns the distance between two points.</p> <p>In your final example, you are assigning a <code>Point</code> to a constant. This behaves a little differently than you might expect. What you <em>cannot</em> do is go ahead and assign a <em>new</em> Point to the <code>MY_POINT</code>. What you <em>can</em> do however is modify the values of that Point. For example, you will be able to do this:</p> <pre><code>MY_POINT.x = 10; </code></pre> <p>Constants are best suited for storing primitive types (int, uint, Number, Boolean, String). In this case you would be better off creating separate constants to store the <code>x</code> and <code>y</code> of your Point, and omit creation of a Point:</p> <pre><code>const POINT_X:int = 10; const POINT_Y:int = 15; </code></pre> <p><br /></p> <p><a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#static" rel="nofollow"><strong><code>static</code></strong></a></p> <p>The <code>static</code> modifier is actually not related to constants, it is a separate keyword that adds the property or method being defined to the class definition rather than instances of that class. Because a constant is stateful and does not change, there is no reason to have a constant attached to every instance of a class.</p> <p>Constants are useful for adding readability and security when working with primitive data. Those constants can be stored as static members of a class acting as a collection of related constants, or a class that makes use of those constants as arguments of its methods.</p> <p>The <code>Event</code> class is a good example of this - it stores primitve data (strings) as static constants to represent the different event types, e.g. <code>Event.ACTIVATE</code>. When using methods that expect an event type, e.g. <code>addEventListener()</code>, we can refer to that constant rather than the primitive <code>"activate"</code>. This protects us against bugs, because the primitive allows spelling errors - if you make a typo accessing a constant, you will receive a compile-time error.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload