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