Wednesday, August 1, 2012

Javascript Find Arguments Passed to a Function

While working on Javascript optimization we can always use some functions having variable number of arguments.
E.g. if we come across a situation where we have to use one function to toggle visibility of the objects, but depending on clients input number of objects which needs to hide/show varies. In this case we can write a function as below where number of parameters can any variable.
//Assumption 0th parameter is boolean 

 function toggle() {
        var args = toggle.arguments
        for (v = 1; v < args.length; v++) {
            obj = window.document.getElementById(args[v]);
            if (args[0]) obj.style.display = "";
            else obj.style.display = "none";
        }
    }
We can use this type of functions to minimize size of javascript before optimizing it.
Submit this story to DotNetKicks

0 comments: