Can anybody show me a use case for the following?
(Flex language manual, Object.constructor)
Advanced users may choose to use the function keyword instead of the class keyword to define a Function object that can be used as a template for creating objects. Such a function is called a constructor function because you can use it in conjunction with the new operator to create objects. If you use the function keyword to create a constructor function, its prototype object is assigned a property named constructor that holds a reference to the constructor function. If you then use the constructor function to create an object, the object inherits the constructor property from the constructor function’s prototype object. For example, the following code creates a new constructor function, f, and an object named myF:
function f() {}
trace(f.prototype.constructor); // function Function() {}
trace(f.prototype.constructor == f); // true
var myF = new f();
trace(myF.constructor == f); // true
UPDATE: I’ve looked into this and I have some answers to my own question:
Dynamic Programming in AS3


I don’t know if there is a particular use case. It’s just how things are made up. That’s how we did “classes” in AS1. I’m glad to leave it behinde, personally. :)
Comment by Keith Peters — January 13, 2008 @ 8:32 pm
In AS1 & AS2, the prototype chain was how classes were actually created, and the inheritance chain was merely a compiler-based syntax. AS3 is no longer a prototype-based language, so the inheritance chain trumps the prototype chain. However, in order to keep ties with the roots of the language (even though there is no direct compatibility between AS1/2 and AS3), the prototype property of a class may still be used. The only use case I can think of for using this legacy remnant of the language, though I’ve never actually tried it — and like Keith I too am happy to see this aspect of the language “go away” — is if you need to create dynamic pseudo-classes from a prototype-based level, in an application such as a Design Patterns simulator, or a UML application which can generate stub code, though there are far better ways to do this through AS3’s new introspection API.
Comment by Joeflash — January 14, 2008 @ 3:27 am
Actually I am using the prototype extensively in my current AS3 work; it’s a great place to store variables that need to be available to every instance of a class and every instance of derived classes. A static property wouldn’t do because it’s not inherited.
My question was really about using a function instead of a class as a constructor. And that indeed does look rather useless.
Comment by alan — January 16, 2008 @ 1:31 am
“it’s a great place to store variables that need to be available to every instance of a class and every instance of derived classes”
Eh, isn’t the answer here “protected”, or am I not following you correctly?
Comment by Dennis — April 27, 2008 @ 6:32 pm
Dennis,
The advantage of saying
is that it’s completely dynamic — the variable does not have to have been declared as a property in SomeClass.as, and it can be set by any code outside the class — and it can be accessed as though it were a property of any instance of SomeClass or of any derived class — either by declaring the class dynamic, or by using the bracket notation:
A protected variable would have to have been declared in the class file, would have to be set on every instance that needs it, and cannot be accessed from unrelated code.
To be more specific about a use case, consider a specific function that a game character must execute whenever he arrives at any Flower, whether it be a Daisy or a Pansy or whatever derived class instance. If I set
then on arrival, my character can call
and the flowerTask is the one that will be executed if the target is any Flower derivative. Of course every potential target class has myTask defined on its prototype or on an ancestor prototype; I set a default myTask on Sprite.prototype to cover the undefined cases.
Comment by alan — April 27, 2008 @ 7:15 pm