Interesting idea, and I'd have to really reflect on this... but my initial
reaction is that it is actually somewhat limiting. Here's what I mean...

In some edge cases it may desirable to use an instance method and bind it to
_some_other_ object. This would certainly be considered "clever" and I can't
think of any concrete cases right now, but I know with the way things are
now it _possible_ if needed. Under this system you would be limiting the
scope binding to only a single instance. I know what you're saying, in that
that IS how traditional classes should be. HOWEVER, one of the things that
makes js development so powerful is this dynamic nature, and the ability to
do things like that when you need to. If this ability starts getting removed
in a core layer of a major abstraction library like prototype, I think it
may have potential to be a "bad thing".

...but that's just my initial reaction. Definitely seems like a reasonable
thing for someone to propose though. I'll be interested to see how this one
plays out...



On 5/10/07, Dan Webb <[EMAIL PROTECTED]> wrote:
>
>
> Hello Everyone,
>
> Just had an idea for a possible enhancement to Class.create() that I
> wanted to float with y'all.   Here's the preamble:
>
> From knocking around on mailing lists / IRC etc it seems that by far
> the most irritating problem for most new JS library users is the idea
> that, in JS, methods of objects have no inherent binding to the object
> they are attached to so if you do something like this:
>
> Car = Class.create();
> Object.extend(Car.prototype, {
> initialize : function(a) {
>    this.a = a;
> },
> showA : function() {
>    console.log(this.a);
> }
> });
>
> This gives the 'expected' result:
>
> a = new Car(7);
> a.showA(); //=> 7
>
> But this doesn't:
>
> func = a.showA
> func(); //=> undefined
>
> This is always a problem inside event handlers and the enumerable
> functions and of course the way to stop it is to use bind().  But I
> think possibly that a point of confusion in Prototype particularly
> that if you use Class.create() you expect a thing that acts like a
> class which it of course doesn't.  So....
>
> How about this:
>
> Class = {};
> Class.create = function() {
> return function() {
>    this.initialize.apply(this, arguments);
>
>    for (var prop in this)
>      if (typeof this[prop] == 'function')
>        this[prop] = this[prop].bind(this);
> };
> }
>
> Which also binds methods of the object to the object itself
> automatically so you can point event handlers etc straight to methods
> of objects and they remain bound....?
>
> Any good?  Any problems Im not thinking of?
>
>
>
> --
> Dan Webb
> http://www.danwebb.net
>
> Event Wax (http://www.eventwax.com)
>
> >
>


-- 
Ryan Gahl
Principal, Manager
Nth Penguin, LLC - Consulting
http://www.nthpenguin.com
--
Software Architect
WebWidgetry.com / MashupStudio.com
Future Home of the World's First Complete Web Platform
--
Inquire: 1-262-951-6727
Blog: http://www.someElement.com

--~--~---------~--~----~------------~-------~--~----~
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