Advanced Users May Choose

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

5 comments to Advanced Users May Choose

  • 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. :)

  • 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.

  • alan

    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.

  • Dennis

    “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?

  • alan

    Dennis,

    The advantage of saying

    SomeClass.prototype.variable = value;
    

    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:

    someClassInstance["variable"]
    

    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

    Flower.prototype.myTask = flowerTask;
    

    then on arrival, my character can call

    target["myTask"]();
    

    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.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>