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.

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