Different amount of variables in same function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kent
    New Member
    • Apr 2007
    • 2

    #1

    Different amount of variables in same function

    I would like to create a function that is variable in the number of variables it processes.

    Example
    function myFunction(myVa riableVar){
    // Loop through the myVariableVar list and do the function
    }


    So when the function is used on different pages like this
    3 variables: myFunction(var1 ,varb,varc);
    or more variables: myFunction(var1 ,varb,varc,var4 ,vard,var7,var5 ,var9);

    bearing in mind that the vars used will be valid elementIDs or elementNames
    I would like help to start the function ie, the "loop command" and i will add the function. If this is not too hard for you script gurus.

    Thankyou
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    /*
    (Moderator: I Edited the try block- my previous post may be deleted)

    This is a an Array method that runs the same function
    on each of its members, and returns an array of the results.
    I include an example function to test- you can adapt it for your purpose.

    *************** ***********/

    Code:
    Array.prototype.forAll= function(fun,args){
    	var L= this.length, A= [], tem;
    	for(var i= 0; i< L; i++){
    		try{
    			if(args===true)tem= fun(this[i]);
    			else tem= fun.apply(this[i],args);
    		}
    		catch(er){
    			tem= null;
    		}
    		if(boo=== true && !tem) continue;
    		A.push(tem);
    	}
    	return A;
    }
    // *************** *********** test function

    Code:
    var fun= function(){
    	return ('\n'+i++) +'. '+this.tagName+
    	'-  descendents: '+ this.getElementsByTagName('*').length;
    }
    function collect(hoo){
    	var collection= [], who,i=0;
    	var L = hoo.length;
    	if (hoo.length) while(i< L) collection.push(hoo[i++]);
    	else if (typeof hoo == "object"){
    		for (var o in hoo) if(hoo.hasOwnProperty(o)) collection.push(hoo[o]);
    	}
    	return collection;
    }
    var A=document.body.getElementsByTagName('*');
    A=collect(A);
    var i=1;

    // *************** *********** And the function command:

    A= A.forAll(fun)
    alert(A.slice(0 ,25));

    Comment

    Working...