You're really going the long way around to achieve something that's quite
simple. See my last reply in this thread.

On 3/30/07, Lorderon <[EMAIL PROTECTED]> wrote:
>
>
> Hi,
>
> The point was to extend the original object with reference to the
> parent object (the original object as before the extension), and
> without creating a new class/object inherited from the original one.
>
> I solved my problem with this:
>
> addPlugin: function(pluginObj) {
>         var $parent = Object.clone(this);
>         for (var p in pluginObj) {
>                 if (typeof(pluginObj[p])=='function')
>                         pluginObj[p] = eval(pluginObj[p].toString());
>         }
>         Object.extend(this,pluginObj);
> }
>
> Notice the addPlugin method defines the $parent object, then it re-
> evaluates the methods in the extension object (pluginObj).
> Wouldn't it better if you could access the parent via a special object
> $parent.show() rather than making:
> this.show.bind(this)();
>
> Where can I find the support forum?
>
> -thanks, Eli
>
>
> On Mar 30, 4:52 pm, "Ryan Gahl" <[EMAIL PROTECTED]> wrote:
> > However, you could employ a slightly more traditional approach, which is
> to
> > leave the first version of A as the base class, and then subclass it as
> > needed:
> >
> > (Btw, the way you have written A below is as a static object, and as
> such
> > you are gaining nothing by using Class.create(). Class.create() points
> the
> > constructor for the class at an "initialize" method on the class's
> > prototype, which of course you never define)
> >
> > Try something like this (notice that I define public class level
> instance
> > methods within the constructor to ensure they are truly only given to
> > instances of the class):
> >
> > var A = Class.create();
> > A.prototype = {
> > initialize: function() {
> > //public instance members
> > this.show = function() {
> > alert("I am A");
> >
> > };
> > }
> > }
> >
> > var B = Class.create();
> > Object.inherit(A, B);
> > Object.extend(B.prototype, {
> > initialize: function() {
> > //base class construction
> > this.base();
> >
> > //override show
> > var oldShow = this.show.bind(this);
> > this.show = function() {
> > oldShow();
> > alert("I am EXTENDED A");
> >
> > };
> > }
> > });
> >
> > var test = new B();
> > B.show(); // alerts "I am A" and "I am EXTENDED A"
> >
> > This technique comes right from by blog post on the inheritance model.
> >
> > Now, if you're looking to tack (or override) methods on existing
> > instances... you don't need a special addPlugin() method like what you
> > started doing. You can just take advantage of plain old javascript...
> >
> > (Assume we are back to using the definition of A from your original
> post)...
> >
> > var oldShow = A.show.bind(A);
> > A.show = function() {
> > oldShow();
> > alert("I am EXTENDED A");
> >
> > };
> >
> > Notice in my first example (the object oriented approach), and in this
> > example (the static object version) I use a .bind() call. Doing so
> ensures
> > that if A.show() ever tried to access "this", it would still point to
> the
> > correct scope.
> >
> > For instance, imagine A.show() looked like this:
> >
> > function() {
> > alert(this.name);
> >
> > }
> >
> > Ok... so I'll stop now I guess, just realize this list isn't really
> supposed
> > to be a support list. :-)
> >
> > Hope this has helped though.
>
>
> >
>


-- 
Ryan Gahl
Application Development Consultant
Athena Group, Inc.
Inquire: 1-920-955-1457
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