Demystifying The Code

ASP.NET AJAX Client Library Question And Answer Series – Question 2

Question: How does Register Class and initializeBase copy child methods into place?

Answer:

This question is probably referring to a warning I give in most discussions regarding calling initializeBase in the constructor function in an implementing class.  In the following screenshot, CallIt.Components.Employee inherits from CallIt.Components.Person.  The important thing to note from this example is the call to initializeBase in the constructor.

ImplClass

The reason I stress the importance to this call has to do with how the ASP.NET AJAX Client Libraries implements inheritance.  For those of you that are familiar with inheritance in JavaScript, you understand that it is quite involved.  There are essentially 3 steps to inherit the functionality of another function:

  1. Call the call function of the base
  2. Set the prototype of the implementing class to the base
  3. Set the constructor of the newly set prototype back to the implementing class (it will be pointing to the base)

In the following example, Employee inherits the functionality of Person, using the JavaScript inheritance steps outlined above:

JSInheritance

ASP.NET AJAX Client Libraries do not follow this pattern, however.  Essentially, our libraries copy the implementations from base classes to implementing classes.  In my WebCasts, I mention how the call to initializeBase is instrumental in this implementation.  Let’s examine…  The first thing to look at is the Type.prototype.resolveInheritance function in the Client Libraries below.  It is clear that this is the code where the members are copied from base to implementing class.

resolveInheritance

Now, if you look at the Type.prototype.resolveInheritance implementation, you will note that it calls resolveInheritance.  If you fail to make this call, under most circumstances, the base members will not be copied to the implementing class.

resolveInheritance

If you have a question you would like to add to the list, please email me at rob.bagby@microsoft.com.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Demystifying The Code