On Jun 29, 8:22 pm, Tobie Langel <[EMAIL PROTECTED]> wrote:
> How is Class a constructor ?

Constructor in JavaScript is a function, which called with
var someObject = new someFunction();
returns an object, which behaves similar to all objects created the
same way.
So in my implementation (as in Prototype) all the classes (the Class
objects), have the similar behaviour - the initialize() method of
their 'prototypes' is the method, run when the object that instantiate
them is created (ie new Animal()).
My Class is a constructor, because it returns such a function. I'm not
sure why is it possible, but it is, that you can overwrite what is
returned when using the 'new' operator in JavaScript. Normally, some
object is created. I return some object too, but this object is a
function in my case - a constructor.
Did I understand the question properly? Did my answer satisfied you?

> Properties applied to it's prototype aren't available on the
> instances.
>
> Am I missing something here ?

Good notice.
No you don't - I did.
I forgot, that object returned from Class, doesn't have the methods
from Class.prototype. In my example it wasn't important, I think - the
initialize() method was the clue.
But adding such a line in Class function, adds such a requirement:

<code>
Object.extend(theConstructor, Class.prototype);
</code>

Full Class code will look now:
<code>
/* constructor returning constructor */
var Class = function(classDef) {
        // this = function() { } // throws error - assigning to 'this' not
allowed (not to mention it would be very ugly)

        var theConstructor = function() {
                this.initialize.apply(this, arguments);
    }

        // Add to the created class, all the methods set under
Class.prototype
        Object.extend(theConstructor, Class.prototype);
        // add classDef to the definition of the class (add all elements of
classDef to 'this')
        Object.extend(theConstructor.prototype, classDef);

        // returning class object constructror, instead of Class object
constructor
        return theConstructor;
};
</code>

So you can do now:
<code>
// standard class methods available in every class (in every Class
object)
Class.prototype.$super = function() {
        document.write('$super(): I am the sublclass of some class...!');
};
</code>

And after creating the Animal class, you can call:
<code>
Animal.$super();
</code>

Which will print:
<output>
$super(): I am the sublclass of some class...!
</output>

Corrected example available, as before, under:
http://dawid.krysiak.net.pl/webdev/js/21.js.constructors.for.constructors.html

Tested on the same set of browsers as before.

BTW: My code editor (jEdit 4.2), doesn't like the '$super' name for
the var name - it founds a "super" keyword (and highlights it like a
keyword), preceded by the $ dollar sign - ugly :-(


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to