Hi DK,
Well that's really exciting! And might allow us to have dynamic class
methods.
I'll have a better look at it as soon as possible - I'm in the middle
of a move right now so it's a bit cmplicated.
Thanks fro your report,
Tobie
On Jun 29, 4:54 am, DK <[EMAIL PROTECTED]> wrote:
> 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.construc...
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---