On Jun 25, 1:07 pm, Tobie Langel <[EMAIL PROTECTED]> wrote:
> It's unfotunately impossible to make a
> constructor for constructors in JS. So Class isn't a constructor here.

Actually, it is possible. See the code below:

<code>
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 classDef to the definition of the class (add all elements
of classDef to 'this')
    Object.extend(theConstructor.prototype, classDef);

    // returning class object constructron, instead of Class object
constructor
    return theConstructor;    // * THE HACK *
};

// creating the Animal class - the Class object called Animal
var Animal = new Class({
    initialize: function() {
        document.write('I am the initializer of Animal object<br />');
    },
    prop1: 'foo',
    say: function() {
        document.write('say(): I am the instance method of the Animal
class<br />');
    }
});
// Animal class - class methods (static methods)
Object.extend(Animal, {
        say: function() {
                document.write('say(): I am the class method of the Animal 
class<br /
>');
        }
});

// creating the Animal object
var my_pet = new Animal();
Animal.say();
my_pet.say();
</code>

The secret is the line marked as * HACK *.
I know, this hack isn't very pretty, but it works in all browsers, I
tested (FF 2.0.0.4, IE6, Opera9, Safari3beta3.0.2). I've checked on
Windows only. Ergo - it IS possible for constructor, to return the
constructor :-)

Full working example:
http://dawid.krysiak.net.pl/webdev/js/21.js.constructors.for.constructors.html


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
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