| |
|
||||
|
OO Glossary
TODO: Very old, stupid and inaccurate - must update or rewrite! Much shorter descriptions, one example, compare with Java if needed.
Summary < < Home > > Downloads
Class Class - In Java, you create a design for an object, and then create objects based on that design. The design is the Class, the objects are instances. In Actionscript a function can be used as a design template, or it can be run directly. Technically there are no classes in Actionscript, but a class is 'somewhat' like a casterated function - one that can only be used as a template. So if you choose to use a function -only- as a template, then you can think of it as a Class. To equate this with Flash, the symbols in the library would be your classes (templates that have not yet been used), each time you drag one onto the work area it becomes an instance (an new object created with a template). You can not see them in your final swf if they are not 'instanciated' by bringing them from the library to the work area (in fact they aren't even exported unless you have brought them on to the stage, or set their linkage in the library). Example: // ********** Class School ********** Here, School is the class, while school1, school2, and school3 are instances of the class School. Most coding conventions have classes starting with capital letters, just so you can tell them apart from instances, which are usually started with small letters. (I guess because classes are more 'important') school1.name is "UTOO" and in this case, we choose to never ask for anything from School directly, because School is a class and therefore it does not exist... well, other than to be our template. Just like we wouldn't try to find the _x coordinate of something in the library. Instance Instance - an object created from a template. The template is a function, often thought of as the class. In flash, everything that ends up in your swf is an instance of something in the library (you don't put big rectangles that aren't symbols in the background do you?! tsk tsk...). Key point: each instance can have different properties - like in Flash, where each instance can be scaled differently, tinted differently, named differently, or have different _x, and _y locations. function Point( x, y ) Arguments Arguments - things that are passed into a function through its parentheses. The items being passed line up one for one with the arguments defined in the function. You should always try to give meaningful names on both sides. Just be aware that a situation like the following will not set x to null, and y to 30: function Point( x, y ) var y = 30; this this - 'this' refers to the instance that has been created, not the 'class' it was created from. So in Flash, say you have a movieclip in your library called (what else) 'Graphic 1', you drag it onto the work area twice, and then give these instances the names 'mc0' and 'mc1'. If you use the word 'this' inside the 'Graphic 1' movieclip, it will refer to the instance 'mc0' in the case of mc0, and the instance 'mc1' in the case of mc1. So if you put trace( this._x ); on frame one of the movieclip (that is, inside the symbol) 'Graphic 1', it will trace the _x location of each clip. In Actionscript it works exactly the same. 'this' refers to the current instance, not the 'class'. So to use the Flash example: function Graphic1 ( x, y, name, xScale, yScale, rotation ) instance0 = new Graphic1( 100, 100, "mc0", 100, 100, 45 ); As you can see, there are now two instances, each with different properties. You can match up each argument being passed with the name of each argument in the 'class' Graphic1 (note there can be no spaces in function names). This of course in no way creates a movieclip! Just a few instances of an Actionscript class, nothing to see. Move along folks, move along... Scope agruments array More about arguments. In Actionscript you can pass a different number of arguments than are defined in the function. Why would you want to do this? Say for example you have a Class that stores people's names. Now everyone knows that normal people have a first name, a middle name, and a last name. So your Class could look like: function setName( first, middle, last ) ..so no problem. Wait. What about those with no middle names. Dang. Ok then, we can pass two arguments if there is no middle name, and three if there is. Then just check if the last value has been passed before setting the middle name: function setName( last, first, middle ) if (middle <> null) Great, it works... What about Japanese names (as well as in many other cultures) that have given names last, and family names first? Probably you want to change 'first' to 'given', and last to 'family'. What about those French inlaws? Big dango. People like Jean Pierre Josheph Debreuil? What about Gaston Bernard Josheph Rene d'Lac au'Fontaine Baguette Debreuil? Now we have a problem. Now we are unsure how many (as well as how few) names we are passing to setName(). Luckily there is a built in object in every function called (what else?) arguments. It is an Object (not an array) that stores the passed arguments in the form: arguments[0] Note that the arguments Object is only available inside the function, which makes sense as passed arguments are local in scope. So you now can do something like this: // pass middle name(s) after family and given trace( person5.middleName.toString() ); If your setName is passed no arguments at all, they can be purchased via an old Monty Python skit - but be sure to get a reciept. And before you think this solves all your problems, what about Chinese names that are generally familyName, givenName, givenName. I guess you can allow spaces in names. (They are normally written with Unicode as well, and Flash 5 does not support Unicode.) What about 'the artist formerly known as Prince'? Like him or not, you have to admire his attempt to screw up every database in the world. I wonder if they've given him his own Unicode symbol? Ok, finally, what would the equivilent be in Flash? How about when you create a new instance of a symbol from the library, you are passing it arguments like _x, _y and _level based on where you place it. Things like _xScale are not set (and get default values, 100 in this case). If you duplicate an instance of a symbol however, all the instance information gets passed to the new instance, so it is somewhat like passing the same function ('createInstanceOfSymbol') and a variable number of arguments, depending on how its being created.
prototype
prototype - the place where a class gets its extra methods/properties from. Same level as the class.
__proto__
__proto__ - the place where an object inherits methods from. One level above the class.
callee
callee - is part of the arguments Object that every function has. You access it as arguments.callee. It returns the current function (not the (string) text of the current function as in Javascript). This makes sense being Flash can not evaluate script at runtime. This might seem to be the same as 'this', but 'this' points to the current instance, not (necessarily) the current function. arguments.callee is only available within the function. arguments.caller is not supported in Actionscript (it refers to the object that called the current function), and deprecated in Javascript. arguments.length is available in Actionscript, and it refers to the number of arguments passed. It will not appear in a 'for in' loop of the arguments Object, however 'callee' will so be careful with that (I don't think callee is supposed to - doesn't in Javascript).
super
super - (java) points to the parent that the current class inherited from. Used to specify which constructor a child should use when calling the parents constructor (must be the first thing to be called, and only used once.) Also often used to get a method or property in the parent that has been overridden by the child. Very similar to __proto__ in Actionscript. constructor
|