Calling base class methods in ASP.NET AJAX
C# or VB.NET is very handy when it comes to inheritance. Because structured programming languages provide a mechanism for inheritance, they also provide a mechanism to call methods, properties, or other constructs defined in the base class, if defined with the correct access level (protected, public, etc.). If a member is private, it cannot be called directly even though the derived class inherited from its base class.
JavaScript has no notion of protected or private scope of methods, and thus any method can be called. Private methods are usually prefixed with an underscore, but this is convention only and doesn't change access to a method. So any method can be called of a component on the client-side. Also, because ASP.NET AJAX supports inheritance, components can call base class methods from derived classes too, just like you have in structured programming languages.
In ASP.NET AJAX, this is done via the callBaseMethod method. For instance, take a look at the initialize and dispose methods of a component:
AjaxSamples.MyComp = function(el) { .. }
AjaxSamples.MyComp.prototype = {
initialize: function() {
AjaxSamples.MyComp.callBaseMethod(this, "initialize");
},
dispose: function() {
AjaxSamples.MyComp.callBaseMethod(this, "dispose");
}
}
AjaxSamples.MyComp.registerClass("AjaxSamples.MyComp", Sys.UI.Control);
Our component calls the initialize and dispose methods in the base class, then define any custom implementations. The callBaseMethod takes a third parameter: an array of parameters to pass to the method, which isn't needed for the initialize or dispose mehods. Parameters can be passed using the following syntax:
AjaxSamples.MyComp.callBaseMethod(this, "doThis", [param]);
Any parameters, whether one or more, have to be defined as an array.
If you have an understanding of developing components in JavaScript, the reasoning may be familiar to you. As you see, JavaScript doesn't support inheritance. The "base." syntax doesn't work because it doesn't know what the base class is. Other developers had created schemes for this, and ASP.NET AJAX has its own approach.
One such was is the use of mixin classes. This type of class mixes in the properties/methods defined in the base class by copying them to the base class directly. An example of this can be found here: http://www.web-articles.info/e/a/title/Implementing-Mixins-in-JavaScript/
This mixin approach copies members from the source to the destination; ASP.NET AJAX doesn't do this; rather, it separates the base type, and calls methods in the base type. This is where callBaseMethod comes into play because it allows you to perform that "base." functionality you have in C#. The other great benefit for ASP.NET AJAX is that the mixin approach pushes off the logic for the component to the new type; with this ASP.NET AJAX approach, the base class can have separated logic from the derived class, and thus separates the logic out. So you can call your base class method and override this method in the derived class to add to it, or override it altogether.