Results 1 to 3 of 3

Thread: JavaScript Parameters Created Dynamically

  1. #1
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default JavaScript Parameters Created Dynamically

    I have a small puzzle I am likely to solve myself quickly. However I will throw the question to the community and see the kind of solutions others would use in this scenario.

    Below is a simple script I created, a function with two parameters. The FOR loop runs if the length of the "example" variable is TRUE.

    When I realized that 'example = function()' contains a length of 0 as the function is counted as one object, and 'example = function(a)' contains a length of 1 as the function and parameter are counted as separate objects.

    QUESTION: Is there a method to construct a function and pass user-created variables as parameters to the function - which therefore would make the function dynamically created?

    I would like to create a user element in which a user prompt (for example) of a number (1 to 5) is passed as a reference to the constructor/prototype function and the user value entered will add parameters to the function.

    <script type="text/javascript">
    var example = function(a, b) {

    for (i=0; i<5; i++) {
    if (example.length) { // the length property of the variable that holds the function should be 0 as only one thing is held.
    // however if the function contains parameters, these will be counted in the length as well
    // in this case, how do we make the parameters passable by reference
    // can we build a prototype function which can add parameters on the fly?
    console.log("That super-computed " + i + "nce");
    }
    else {
    console.log("that didn't compute");
    }
    }
    }

    window.onload = example();
    </script>

  2. #2
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default

    I self-solved.

    Do people read this stuff??

    Code:
    <script type="text/javascript">
    
    var holdMeOutside = []; // global variable holds function objects passed out
    
    var example = function(a, b) {
       var holdMeInside = []; // private variable holds objects passed within
    	  
    	   function choose() { // inner function
    		for (i=0;i<5;i++) 
    			{
    			  var choice = prompt("enter a character from a-z"); // make this easier with a for loop reiteration up to 5 or less choices
    																	 // a return value of nothing will end the loop
    			  if (choice.length >=0) { // do something looped
    				 // holdMeOutside += holdMeOutside.push(choice); // this whole line failed, below line works
    				 holdMeInside.push(choice); // add a value to the array
    				 alert('array length:' + holdMeInside.length + 'params: ' + holdMeInside.toString());
    				}
    			}		
    			// outside the FOR loop within the choose() function ... create new function
    			var constructIt = new Function([holdMeInside], 'alert("perform a function")'); // if array holds number, "unexpected number" exception thrown
    			alert(constructIt.toString());
    			alert(constructIt.length);
    			
    			
    	   }
     
       for (j=0; j<2; j++) {
    
         if (example.length) { // the length property of the variable that holds the function should be 0 as only one thing is held.  
    	 					   // however if the function contains parameters, these will be counted in the length as well
    						   // in this case, how do we make the parameters passable by reference
    						   // can we build a prototype function which can add parameters on the fly?
             console.log("That super-computed " + j + "nce");
         }
         else {
             console.log("that didn't compute");
         }
    	 choose();
       }
    }
    
    window.onload = example();
    </script>

  3. #3
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    118
    Thanks
    16
    Thanked 1 Time in 1 Post

    Default

    I guess not. I should thank myself

Similar Threads

  1. Replies: 8
    Last Post: 02-28-2011, 05:02 PM
  2. Replies: 2
    Last Post: 08-11-2008, 02:52 PM
  3. Dynamically changing dynamically created data
    By gsibble in forum JavaScript
    Replies: 0
    Last Post: 06-08-2006, 02:05 AM
  4. How to Add BR in dynamically created rows
    By pskumar in forum JavaScript
    Replies: 1
    Last Post: 12-17-2005, 07:49 AM
  5. Passing parameters dynamically in Pop-it Menu
    By amaz0n in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 06-30-2005, 07:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •