Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The question seems to me to be equivalent to,"Why can I use a class as a value?" It's a good question. </p> <p>There are two major things you can do with a class in ActionScript; you can <em>instantiate</em> it, and you can <em>access static properites</em> of it. (Okay, there are other things, but those are the obvious ones.) Instantiating a class is probably the most relevant use of a class as a value; the Class is its own <a href="http://www.c2.com/cgi/wiki?FactoryPattern" rel="nofollow noreferrer" title="Factory Pattern">factory</a>, like ones you would make explicitly in C++ or Java.</p> <p>Here's an example from a small Tetris game I was writing as an exercise. I had two classes, Brick and SceneBrick. A Brick was an in-game brick, the ones that fall and pile up. A SceneBrick was a brick just drawn in as the frame of the play area.</p> <p>However, I used one function to draw both of them, without referring to either type within the function:</p> <pre><code>function drawBricks(xs:Array, ys:Array, brickType:Class){ xs.map(function(o,i,a){ var brick = new brickType(); // etc. }); } </code></pre> <p>And I could use that like this:</p> <pre><code>drawBricks([0,1,2,3], [4,4,4,4], Brick); // draw a long piece drawBricks(times(0, 21), countFrom(0, 21), SceneBrick); // draw a big vertical "wall" </code></pre> <p>In ActionScript, you can pass around a class as a value and use it to instantiate that class. In a language like Java or C++, you would have to either use it as a parameter for a generic class or define factory classes with some common supertype in order to do this.</p>
 

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