Syntax and Semiotics. The Use of the $ in ActionScript.
…or, Apparently Appropriate Appropriation Appears Appealingly Apropos.
Maybe.
The prototype.js library, brought to the attention of the world—the ray of sunshine—the hope for a new tomorrow—the coke and the smile that is the almighty Super-Duper Dollar-Sign™ ($) that, when used either alone or with a second instance1, has taken the tedium of out of obtaining a pointer to one or more DOM entities, either by name or css style, respectively. What I've not seen, is the same clever approach in ActionScript (of any version).
I'm sure it's out there somewhere, but remains rare, indeed… enough to make me wonder if there isn't a good reason why, and if not, why more folks aren't doing it. [As an aside, I'm often wondering why I'm not "doing it", but then remember that I'm at work and that sort of thing is, unfortunately, frowned upon by the fuddy-duddy bean counters.]Maybe it just hasn't found the same Sweet Spot Of Success™ that it has in the world of browser JS DOM manipulation. Or maybe everyone is still busy being productive with AS3 deployed on the Flash Player, that they haven't yet uncovered the need.But I have found one decent use, I think.Or, at least, a reasonable place to do such a thing, until something better comes along, or Adobe makes me very happy this Christmas and delivers operator overloading and definition in my ActionScript stocking (I'm not holding my breath).Consider a two-dimensional vector class in both C++ and AS3, called Vector. There is no member functions in either class beyond the constructor. We want to add the ability to add one Vector to another. In C++, we define an operator for the class that would take the other Vector as an argument, and return the sum. In AS3, however, we have to improvise... (assume A, B, and C are all instances of this same Vector class): C++: C = A + B;AS3: C = A.add(B);Or, to be more semantically correct, we might say: AS3: C = A.plus(B);But what if we want to not only add another Vector, but a number to each member variable of the class. In C++, we would override the + operator by specifying a different argument type (the type of the number to be added), while in AS3, we'd have to figure out another function name: C++: C = A + 3;AS3: C = A.plusNumber(3);I'm sure you could think of a better word choice, but the fact remains, while plus is reasonably semantic, plusNumber, while arguably reasonable, is clumsy--especially when next C++'s elegant option.In fact, anything we do is going to be less elegant, so we might as well do something that a) makes sense logically; b) has some semantic integrity; c) perhaps gives up a little intuitiveness for pithiness, like: AS3: C = A.$plus(3);Not great as it stands, but wait...Imagine we want to be able to add two numbers, one to each of the member variables. In C++, we could overload the constructor which would allow us to write it like this: C++: C = A + Vector(3, 5);In AS3, we could continue on the phunky phrase train with: AS3: C = A.plusNumbers(3, 5);Or we could expand our post-modern function-naming scheme to allow: AS3: C = A.$$plus(3, 5);If this convention is extended throughout the other arithmetic operator simulators, we'd wind up with a logical, semantically reasonable, not ALL that inelegant solution that, arguably, beats the socks off the alternatives: C = A.plus(B);C = A.$plus(3);C = A.$$plus(3, 5); C = A.minus(B);C = A.$minus(3);C = A.$$minus(3, 5);...which, I'd hope you agree is much better than something like: C = A.plus(B);C = A.plusNumber(3);C = A.plusNumbers(3, 5); C = A.minus(B);C = A.minusNumber(3);C = A.minusNumbers(3, 5);...But, with an adjustment to the semantics (plus meaning the addition TO the Vector itself), you get: C = A.addedTo(B); C = A.$addedTo(3); C = A.$$addedTo(3, 5);and: C = A.addedTo(B);A.plus(B);trace(C == A); // true C = A.$addedTo(3);A.$plus(3);trace(C == A); // true C = A.$$addedTo(3, 5);A.$$plus(3, 5);trace(C == A); // trueMakes some sense, eh? The same thing happens in the three and four-dimensional vectors, and a similar thing happens with the matrix classes. We only have eight simple words/phrases to remember (addedTo, subtractedFrom, dividedBy, multipliedBy, plus, minus, divided, multiplied... and yes, the division and multiplication semantics are questionable in this example), and the number of dollar-signs indicate the number of arguments that are of the same type as the class's elements.There are likely many, many, many other clever uses, but those I will leave as an exercise to the reader... one I hope they will share with the rest of the class... (please?).
- becoming the almighty Super-Duper Double-Dollar-Sign™ (
$$) [↩]
about this entry
you’re currently skimming and ignoring “Syntax and Semiotics. The Use of the $ in ActionScript.,”
- published:
- 08.02.07 / 9pm
- category:
- flex
4 comments
click to care. click to comment. | comments rss [?] | trackback uri [?]