PDA

View Full Version : JavaScript Parameters Created Dynamically



sniperman
03-18-2015, 03:53 PM
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>

sniperman
03-18-2015, 05:57 PM
I self-solved. ;)

Do people read this stuff??


<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>

sniperman
03-21-2015, 07:31 PM
I guess not. I should thank myself