Blog Customizations, Part 3: OO JavaScript



Not satisfied by the search feature provided by Community Server I’ve again created some helpful JavaScript classes this time using object-oriented JavaScript to define a basic search provider and to subclass that for specialized providers like MSN Search.

I defined several private properties and several privileged methods that can access those private properties, unlike public methods in classical object-oriented languages. Thanks to closures those methods can access those private properties — defined as local variables — even after the constructor function has returned.

To define a subclass we use the base class to the prototype property of the subclass. We must also assign a new function to the constructor otherwise when the subclass is instantiated the base class constructor is called.

The constructor now creates an instance of the specialized MsnSearchProvider and to call the base class constructor by calling the call method on the constructor. All that’s left to do is to replace property values and event handlers for the search-related elements. For this another class is created.

The this object must be closed so that the event handlers can access it; otherwise, when the event handler was called this would refer to the element on which the event was fired. Simply pass in the element IDs of the search textbox and link to perform the search.

While object-oriented JavaScript uses prototypal inheritance instead of classical inheritance, it is still a powerful tool. You can find lots of information about JavaScript inheritance and JavaScript closures that can help you learn more about object-oriented JavaScript.

Heath Stewart

Senior Software Engineer

Follow Heath   


    Leave a comment