On Mar 28, 9:43 pm, "T.J. Crowder" <[EMAIL PROTECTED]> wrote:
> Hi,
> > A function or property refering directly to the name/objectindex of
> > the obejct/class instance would be much more practical.

That infers that you are going to execute logic based on the "name" of
the constructor.  One of the objectives of OO programming is that you
don't care what the constructor was, you test if the object has the
method you want to use (or implement logic so that you know that the
method exists) and then call it, the method should already be set to
the appropriate function for that object.


[...]
> But based on your later comments, I don't think you're looking for the
> name of the variable pointing to an instance anyway.  You're looking
> for the name of the constructor function (which people sometimes call
> a "class").  E.g., in the non-Prototype world:
>
> function MyNiftyThing()
> {
>     // ....}
>
> MyNiftyThing.prototype.makeSomeElements = funtion()
> {
>     // make some elements}
>
> You want to know that the elements were created by MyNiftyThing.  And
> indeed, in the non-Prototype world, Mozilla's constructor.name
> property would tell you that, but as far as I know that's not
> supported by anyone else and even if it were, the way Class.create
> works, Mozilla's custom property would always return "klass" anyway.

The constructor.name property is easily implemented (as you do below).


> Here's how to do what I think you want to do, both non-Prototype and
> Prototype (tested on FF, IE6, Opera9, Safari 3 Windows beta):
>
> * * * *
> // Non-Prototype
> function MyNiftyThing()
> {
>     // Remember the name of the constructor function using a custom
>     // 'ctorName' property
>     arguments.callee.ctorName = 'MyNiftyThing';}

It doesn't seem sensible to set the constructor's name property every
time it is called.  It makes more sense to set it once on the
constructor's prototype.  :-)

>
> MyNiftyThing.prototype.getCtorName = function()
> {
>     return this.constructor.ctorName;
>
> }
>
> // Prototype
> var MyPrototypeThing = Class.create({
>     initialize: function() {
>         this.constructor.theClass = 'MyPrototypeThing';
>     },
>     getCtorName: function()
>     {
>         return this.constructor.ctorName;
>     }});
>
> * * * *
>
> Strictly speaking you don't need the getCtorName function, anything
> with access to one of the instance of the class can see the name
> directly:
> * * * *
> var t;
> t = new MyPrototypeThing();
> alert("It's constructor name is " + t.constructor.ctorName);
> * * * *
>
> But it's better to encapsulate that using a function.

Why? It doesn't offer anything that isn't provided by:

  MyNiftyThing.prototype.ctorName = 'whatever';

They are both public properties that are easily overridden.  One use
for a function is to create the ctorName property as a private member,
then the get function can be a privileged function:

<URL: http://www.crockford.com/javascript/private.html >


--
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to rubyonrails-spinoffs@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to