Click to See Complete Forum and Search --> : array threw functions or name


Code One
11-18-2003, 05:34 PM
is it possible to array threw javascript functions, and or names. Meaning : Id like to setup an array that would run threw each specified function name, or name, and would then run each function one by one.

ex. function
<script>
function reset(){
}
</script>

<script>
array func=new array(1)
func[1]="reset()"
</script>
-------------------------
ex. name
<script>
getelementbyid('postit').document.form.value;{
}
</script>

<script>
array func=new array(1)
func[1]="postit"
</script>
<body>
<textarea name="postit"></textarea>
-------------------------
Ok I know my arrays need work, but thats why I am asking you how to do this, I obviously have no clue. Is this possible, and if so, how do I set this up?


thanks

Code One

Charles
11-18-2003, 09:07 PM
In client side JavaScript functions are simply properties of some object. And they can be assigned as properties of an Array object. What you need to know, though, is that "functionName()" calls the function and returns the value while "functionName" is the function itself.

<script type="text/javascript">
<!--
function foo () {return 'fooString'}

my array = new Array ();
array [0] = foo // and array [0] now contains the function foo and you can use it with array [0] ()
array [0] = foo () // and array [0] now caontains the string 'fooString'
// -->
</script>

But why on earth would you use such a thing? Just string the functions together as lines of some other function. instead of

function foo () {
array = [fee, fie, foe, fum];
for (var j=0; j<array.length; j++) array[j]();
}

Just use

function foo () {fee(); fie(); foe(); fum()}

Code One
11-18-2003, 09:35 PM
Perfect!!!

Thank you,

Code One :)