Tags

, , , ,

Method overloading in JavaScript is well known technique among the C# developers. However, as popular as it is for server side coders, is not known at all by JavaScript programmers…. And JavaScript become more and more required in SharePoint development.

Let’s assume we have the code below and we are required to perform different action (return different objects types maybe) depending by parameters type:

function overload(param1, param2) {
    
    // Do something if param1 and param2 are numbers

    // Do something else if param1 is string and param2 is missing
}

To achieve this, we might get a little help from SharePoint base library, which is Microsoft Ajax, a very good and old friend for SharePoint Developers. More exactly, this library provides a way to validate parameters in JavaScript. Using Function.validateParameters we can apply different scenarios in our function. Let’s take a look below how:

function overload(param1, param2) {

    // Check if param1 and param2 are numbers
    var e = Function.validateParameters(arguments,[{ name: 'param1', type: Number, mayBeNull: false, optional: false},
                                                   { name: 'param2', type: Number, mayBeNull: false, optional: false }], true);

    // Check if param1 is string and param2 is missing
    var ee = Function.validateParameters(arguments, [{ name: 'param1', type: String, mayBeNull: false, optional: false }], true);

    // Throw an error if parameters are not in expected type
    if (e !== null && ee !== null) {
        throw 'Invalid parameters';
    }

    if(e === null) {
        // Do something if param1 and param2 are numbers
    }

    if( ee === null) {
        // Do something else if param1 is string and param2 is missing
    }
}

Unfortunately documentation is confusing for this function on MSDN. According to the documentation, it should return true if the parameters are validated, otherwise null. In fact it is returning null if parameters are valid and string containing error message if not. Error message is actually generated by another Microsoft Ajax JavaScript object called Error, but this is another discussion.

The example above is simple and does not require additional comments. However, one thing is missing here and this is intellisense. As we are using Microsoft technologies and Visual Studio, we should provide a valid intellisense for it.

function overload(param1, param2) {
    /// <signature>
    ///     <summary>This time param1 and param2 are numbers</summary>
    ///     <param name="param1" type="Number" optional="false" mayBeNull="false">Param 1</param>
    ///     <param name="param2" type="Number" optional="false" mayBeNull="false">Param 2</param> 
    /// </signature>
    /// <signature>
    ///     <summary>This time param1 is string and param2 is not provided</summary>
    ///     <param name="param1" type="String" optional="false" mayBeNull="false">Param 1</param>
    /// </signature>

    // Check if param1 and param2 are numbers
    var e = Function.validateParameters(arguments,[{ name: 'param1', type: Number, mayBeNull: false, optional: false},
                                                   { name: 'param2', type: Number, mayBeNull: false, optional: false }], true);

    // Check if param1 is string and param2 is missing
    var ee = Function.validateParameters(arguments, [{ name: 'param1', type: String, mayBeNull: false, optional: false }], true);

    // Throw an error if parameters are not in expected type
    if (e !== null && ee !== null) {
        throw 'Invalid parameters';
    }

    if(e === null) {
        // Do something if param1 and param2 are numbers
    }

    if( ee === null) {
        // Do something else if param1 is string and param2 is missing
    }
}

Providing XML comments on the beginning of the function will enable Visual Studio intellisense, showing information about both signatures. I was not able to provide a screenshot, but if you try yourself you will know I am talking about.

Hoping this will help you to organize your JavaScript code better, the last thing I need to say is “Happy Coding”.

Advertisements